#include <iostream> #include <cmath> using namespace std; struct Point { int x,y; }; double distance(Point a,Point b) { return sqrt(pow(a.x-b.x,2)+pow(a.y-b.y,2)); } int main() { int n,m,a,b; cin>>n>>m; Point p[1001],ask; for(int i=0;i<n;i++) cin>>p[i].x>>p[i].y; while(m--) { cin>>ask.x>>ask.y; double dist=distance(p[0],ask); int flag=0; for(int i=0;i<n;i++) { if(dist>distance(p[i],ask)) { flag=i; dist=distance(p[i],ask); } } cout<<flag+1<<endl; } }
2020年8月25日 星期二
b976: 5.最終任務->a.尋找提示
程式碼:
b978: 7.最終任務->c.重組提示
程式碼:
#include <iostream> using namespace std; int main() { string s,ss; int command; while(cin>>s) { ss=s; for(int i=0;i<s.size();i++) { cin>>command; ss[command-1]=s[i]; } cout<<ss<<endl; } }
c508: 去蟲
解題心得:
map反過來輸入的寫法:
for(map<int,int>::reverse_iterator rit=m.begin();rit!=m.rend();rit++)
cout<<rit->first<<endl;
程式碼:
map反過來輸入的寫法:
for(map<int,int>::reverse_iterator rit=m.begin();rit!=m.rend();rit++)
cout<<rit->first<<endl;
程式碼:
#include <iostream> #include <algorithm> #include <map> using namespace std; int main() { int n,arr[100001]; map<int,int> m; map<int,int>::reverse_iterator it; cin>>n; for(int i=0;i<n;i++) cin>>arr[i]; for(int i=0;i<n;i++) m[arr[i]]++; sort(arr,arr+n); for(int i=0;i<n;i++) cout<<arr[i]<<" "; cout<<endl; for(it=m.rbegin();it!=m.rend();it++) cout<<it->first<<" "; }
2020年8月24日 星期一
10905 - Children's Game
程式碼:
#include <iostream> #include <algorithm> using namespace std; bool cmp(string a,string b) { return (a+b)>(b+a); } int main() { int n; while(cin>>n) { if(n==0) break; string l[51]; for(int i=0;i<n;i++) cin>>l[i]; sort(l,l+n,cmp); for(int i=0;i<n;i++) cout<<l[i]; cout<<endl; } return 0; }
d550: 物件排序
解題心得:
可以用內建sort,但要稍微優化IO。
vector二維初始化方式。
程式碼:
可以用內建sort,但要稍微優化IO。
vector二維初始化方式。
程式碼:
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); int n,m,tmp; while(cin>>n>>m) { vector<vector<int>> l; for(int i=0;i<n;i++) { vector<int> ll; for(int j=0;j<m;j++) { cin>>tmp; ll.push_back(tmp); } l.push_back(ll); } sort(l.begin(),l.end()); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) cout<<l[i][j]<<" "; cout<<'\n'; } } return 0; }
2020年8月23日 星期日
b265: Q11286 - Conformity
程式碼:
#include <iostream> #include <map> #include <algorithm> using namespace std; int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); int n,tmp,index[5]; while(cin>>n) { if(n==0) break; map<string,int> m; map<string,int>::iterator it; while(n--) { string temp; for(int i=0;i<5;i++) cin>>index[i]; sort(index,index+5); for(int i=0;i<5;i++) temp+=to_string(index[i]); it=m.find(temp); if(it!=m.end()) (it->second)++; else m[temp]=1; } int max=0,ans=0; for(it=m.begin();it!=m.end();it++) if(it->second>max) max=it->second; for(it=m.begin();it!=m.end();it++) if(it->second==max) ans+=max; cout<<ans<<'\n'; } return 0; }
d517: 文字抄寫 I
程式碼:
#include <iostream> #include <map> using namespace std; int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); int n; string temp; while(cin>>n) { int len=1; map<string,int> m; map<string,int>::iterator it; while(n--) { cin>>temp; it=m.find(temp); if(it!=m.end()) cout<<"Old! "<<it->second<<'\n'; else { cout<<"New! "<<len<<'\n'; m[temp]=len++; } } } return 0; }
2020年8月20日 星期四
d502: 第三題:產品包裝
解題心得:
邊長=4的每個自己一箱,沒有剩餘空間。
邊長=3的每個自己一箱,每個剩下4*4*4-3*3*3 = 37 的空間
邊長=2的一箱可裝8個,若無法裝滿則剩下64-8*(b%8) 的空間
邊長=1的則是見縫插針,如果上面剩的空間夠就不用多一箱,不夠就多一箱。
程式碼:
邊長=4的每個自己一箱,沒有剩餘空間。
邊長=3的每個自己一箱,每個剩下4*4*4-3*3*3 = 37 的空間
邊長=2的一箱可裝8個,若無法裝滿則剩下64-8*(b%8) 的空間
邊長=1的則是見縫插針,如果上面剩的空間夠就不用多一箱,不夠就多一箱。
程式碼:
#include <iostream> using namespace std; int main() { int a,b,c,d; while(cin>>a>>b>>c>>d) { int ans=0,space=0; ans+=d; ans+=c; space+=37*c; ans+=(b%8==0?b/8:b/8+1); space+=64-8*(b%8); if(a>space) ans++; cout<<ans<<endl; } return 0; }
c776: 106北二1.六邊形屋瓦
解題心得:
國高中數學題變形。
先觀察n=1時,a1=6, d=4
而n每增加1,a1就增加5,因此a1 = 6+(n-1)*5 = 5n+1
至於公差d,則是每次增加3,因此d = 4+(n-1)*3 = 3*n+1
所求由等比公式得知為: a1+(m-1)*d = (5n+1)+(m-1)(3n+1)
程式碼:
國高中數學題變形。
先觀察n=1時,a1=6, d=4
而n每增加1,a1就增加5,因此a1 = 6+(n-1)*5 = 5n+1
至於公差d,則是每次增加3,因此d = 4+(n-1)*3 = 3*n+1
所求由等比公式得知為: a1+(m-1)*d = (5n+1)+(m-1)(3n+1)
程式碼:
#include <iostream> using namespace std; int main() { int n,m; cin>>n>>m; cout<<(5*n+1)+(m-1)*(3*n+1)<<endl; return 0; }
2020年8月18日 星期二
operator overloading
自定義物件的加減乘除等操作。
operator+
例如:
const Complex operator+(const Complex &obj); //member function
使用const+參考是為了效率。
還可以寫成friend function:
friend Comples operator+(Complex &o1,Complex &o2);
記得在外部定義即可:
Complex operator+(Complex &o1,Complex &o2)
{
return Complex(o1.real+o2.real,o1.imag+o2.imag); // 回傳沒有名字的物件
}
(note:
由上面的例子可得知,constructor本身並非void function,它會回傳沒有名字的物件。)
上面討論的都是 c3=c1+c2,也就是兩個參數都是object的時候。
如果是c3=c1+x呢?(x為任意int)
Complex Complex::operator+(int& x); //裡面傳的參數資料型態改一下
如果是c3=x+c2呢?
原本上面兩個狀況都是由c1這個物件呼叫operator+這個函式,有點像c1.operator+(c2)的感覺,所以才用member function。
現在則沒有物件能呼叫了,所以才用外部定義的friend function。
Complex operator+(int& x,Complex& o);
(note:
c3=(c1+c2) // c3 和 (c1+c2) 是不同物件。(c1+c2)是一個沒有名字的物件,它的值會被拿去給c3)
operator++
有prefix跟postfix兩種,為了區別於是寫法如下,只差在有沒有再傳參數進去。
Point& operator++() // prefix
{
count++;
return *this;
}
(note: 回傳值為參考)
Point operator++(int n) // postfix
{
Point tmp=*this;
this->count++;
return tmp;
}
(note: postfix 的 operator++回傳值不是參考!!否則會出現錯誤訊息:"warning: reference to local variable ‘result’ returned [-Wreturn-local-addr] ",因為若回傳tmp參考,tmp本身會再動作結束後消失造成錯誤!!)
可以簡化來看:
參考:overloading postfix and prefix operators
(the function overloading can be achieved by different data type and different number of argument list, but it cannot be different return type.)
operator>> & <<
friend istream& operator>>(istream& in,Complex &c)
{
in>>c.real>>c.imag;
return in;
}
friend ostream& operator<<(ostream& out,Complex &c)
{
out<<c.real<<" "<<c.imag<<endl;
return out;
}
(note:return in跟return out是為了連續輸入/輸出)
operator+
例如:
const Complex operator+(const Complex &obj); //member function
使用const+參考是為了效率。
還可以寫成friend function:
friend Comples operator+(Complex &o1,Complex &o2);
記得在外部定義即可:
Complex operator+(Complex &o1,Complex &o2)
{
return Complex(o1.real+o2.real,o1.imag+o2.imag); // 回傳沒有名字的物件
}
(note:
由上面的例子可得知,constructor本身並非void function,它會回傳沒有名字的物件。)
上面討論的都是 c3=c1+c2,也就是兩個參數都是object的時候。
如果是c3=c1+x呢?(x為任意int)
Complex Complex::operator+(int& x); //裡面傳的參數資料型態改一下
如果是c3=x+c2呢?
原本上面兩個狀況都是由c1這個物件呼叫operator+這個函式,有點像c1.operator+(c2)的感覺,所以才用member function。
現在則沒有物件能呼叫了,所以才用外部定義的friend function。
Complex operator+(int& x,Complex& o);
(note:
c3=(c1+c2) // c3 和 (c1+c2) 是不同物件。(c1+c2)是一個沒有名字的物件,它的值會被拿去給c3)
operator++
有prefix跟postfix兩種,為了區別於是寫法如下,只差在有沒有再傳參數進去。
Point& operator++() // prefix
{
count++;
return *this;
}
(note: 回傳值為參考)
Point operator++(int n) // postfix
{
Point tmp=*this;
this->count++;
return tmp;
}
(note: postfix 的 operator++回傳值不是參考!!否則會出現錯誤訊息:"warning: reference to local variable ‘result’ returned [-Wreturn-local-addr] ",因為若回傳tmp參考,tmp本身會再動作結束後消失造成錯誤!!)
可以簡化來看:
operator++()
operator++(int)
值得注意的是為何postfix operator需要額外的傳int進去呢?這是因為C++判斷function的時候,是由它們的signature,也就是參數數量、型別與順序,而非回傳值來看。參考:overloading postfix and prefix operators
(the function overloading can be achieved by different data type and different number of argument list, but it cannot be different return type.)
operator>> & <<
friend istream& operator>>(istream& in,Complex &c)
{
in>>c.real>>c.imag;
return in;
}
friend ostream& operator<<(ostream& out,Complex &c)
{
out<<c.real<<" "<<c.imag<<endl;
return out;
}
(note:return in跟return out是為了連續輸入/輸出)
2020年8月16日 星期日
a870: 10. List Maker
解題心得:
vector find: find(vec.begin(),vec.end(),x);
vector insert: vec.insert(it,x); // or vec.insert(vec.begin()+n,x);
vector erase: vec.erase(it) // or vec.erase(vec.begin()+n);
程式碼:
vector find: find(vec.begin(),vec.end(),x);
vector insert: vec.insert(it,x); // or vec.insert(vec.begin()+n,x);
vector erase: vec.erase(it) // or vec.erase(vec.begin()+n);
程式碼:
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { string command,x,n; vector<string> l; while(cin>>command) { if(command=="SHOW") break; if(command=="ADD") { cin>>x; l.push_back(x); } else if(command=="INSERT") { cin>>x>>n; vector<string>::iterator it=find(l.begin(),l.end(),n); l.insert(it,x); } else if(command=="REMOVE") { cin>>x; for(int i=0;i<l.size();i++) { if(l[i]==x) { l.erase(l.begin()+i); break; } } } } for(int i=0;i<l.size();i++) cout<<l[i]<<" "; return 0; }
a787: 9. Mirror to the Stars
程式碼:
#include <iostream> using namespace std; int main() { string name,command; int width,height; char picture[37][25]; while(cin>>name>>width>>height>>command) { for(int i=0;i<height;i++) for(int j=0;j<width;j++) cin>>picture[i][j]; cout<<name<<endl; for(int l=0;l<command.size();l++) { char copy[37][25]; for(int i=0;i<height;i++) for(int j=0;j<width;j++) copy[i][j]=picture[i][j]; if(command[l]=='R') { for(int i=0;i<height;i++) for(int j=0;j<width;j++) picture[i][j]=copy[i][width-1-j]; } else { for(int i=0;i<height;i++) for(int j=0;j<width;j++) picture[i][j]=copy[height-1-i][j]; } } for(int i=0;i<height;i++) { for(int j=0;j<width;j++) cout<<picture[i][j]; cout<<endl; } } return 0; }
2020年8月14日 星期五
12918 - Lucky Thief
程式碼:
#include <iostream> using namespace std; int main() { int t,n,m; cin>>t; while(t--) { cin>>n>>m; long long int up=m-1,down=m-n; cout<<(up+down)*n/2<<endl; } return 0; }
11498 - Division of Nlogonia
程式碼:
#include <iostream> using namespace std; int main() { int k,n,m,x,y; while(cin>>k) { if(k==0) break; cin>>n>>m; for(int i=0;i<k;i++) { cin>>x>>y; if(x==n||y==m) cout<<"divisa"<<endl; else if(x>n&&y>m) cout<<"NE"<<endl; else if(x>n&&y<m) cout<<"SE"<<endl; else if(x<n&&y>m) cout<<"NO"<<endl; else cout<<"SO"<<endl; } } return 0; }
263 - Number Chains
程式碼:
#include <iostream> #include <vector> using namespace std; int main() { string s; while(cin>>s) { if(s=="0") break; cout<<"Original number was "<<s<<endl; string bigToSmall=s,smallToBig=s; int chain=0; vector<string> list; while(1) { chain++; bigToSmall=s,smallToBig=s; for(int i=0;i<bigToSmall.size();i++) { for(int j=0;j<bigToSmall.size();j++) { if(bigToSmall[i]>bigToSmall[j]) swap(bigToSmall[i],bigToSmall[j]); if(smallToBig[i]<smallToBig[j]) swap(smallToBig[i],smallToBig[j]); } } int big=stoi(bigToSmall),small=stoi(smallToBig),repeat=0; s=to_string(big-small); cout<<big<<" - "<<small<<" = "<<s<<endl; for(int i=0;i<list.size();i++) { if(s==list[i]) { repeat=1; break; } } if(repeat) break; list.push_back(s); } cout<<"Chain length "<<chain<<endl<<endl; } return 0; }
friend
若某個函式是某class的friend,那個函式就可以存取到private的資料。
它會被宣告在class外面,就像一般的函式一樣。不過在class裡面會多加一個friend在前面。
寫在private或public部分沒有影響,畢竟它不是member function,只是friend。
class CPoint
{
private:
int x,y;
friend CPoint offset(CPoint &pt,int diff);
public:
CPoint(){x=0,y=0;}
CPoint(int a,int b){x=a,y=b;}
void Print(){cout<<x<<" "<<y<<endl;}
};
CPoint offset(CPoint &pt,int diff)
{
pt.x+=diff;pt.y+=diff;
return pt;
}
int main()
{
CPoint p1(3,4);
p1.Print();
offset(p1,10);
p1.Print();
return 0;
}
friend除了用在function上,還能用在class。
如果:
class A
{
friend class B;
// other code....
};
那class B裡的所有member function都能取用class A的private資料。
但friend的關係並非雙向!
它會被宣告在class外面,就像一般的函式一樣。不過在class裡面會多加一個friend在前面。
寫在private或public部分沒有影響,畢竟它不是member function,只是friend。
class CPoint
{
private:
int x,y;
friend CPoint offset(CPoint &pt,int diff);
public:
CPoint(){x=0,y=0;}
CPoint(int a,int b){x=a,y=b;}
void Print(){cout<<x<<" "<<y<<endl;}
};
CPoint offset(CPoint &pt,int diff)
{
pt.x+=diff;pt.y+=diff;
return pt;
}
int main()
{
CPoint p1(3,4);
p1.Print();
offset(p1,10);
p1.Print();
return 0;
}
friend除了用在function上,還能用在class。
如果:
class A
{
friend class B;
// other code....
};
那class B裡的所有member function都能取用class A的private資料。
但friend的關係並非雙向!
2020年8月13日 星期四
【Hamilton】The World Was Wide Enough 歌詞筆記
sign on:擔任
number two:副手
terrain:地形
marksman:神槍手
rigor:嚴格的
methodically:有條不紊的
fiddle:撥弄
seconds:副手
send in:派出
set the record straight:澄清是非
take aim:瞄準
wise up:領悟
usher:迎來
wailing:哀號、哭泣
obliterate:抹去不留痕跡
number two:副手
terrain:地形
marksman:神槍手
rigor:嚴格的
methodically:有條不紊的
fiddle:撥弄
seconds:副手
send in:派出
set the record straight:澄清是非
take aim:瞄準
wise up:領悟
usher:迎來
wailing:哀號、哭泣
obliterate:抹去不留痕跡
10093 - An Easy Problem!
解題心得:
解題推導參考網路。
程式碼:
解題推導參考網路。
程式碼:
#include <iostream> #include <string> using namespace std; int main() { string s; while(getline(cin,s)) { int sum=0,temp,max=1; for(int i=0;i<s.size();i++) { if('0'<=s[i]&&s[i]<='9') temp=s[i]-'0'; else if('A'<=s[i]&&s[i]<='Z') temp=s[i]-'A'+10; else if('a'<=s[i]&&s[i]<='z') temp=s[i]-'a'+36; else continue; if(temp>max) max=temp; sum+=temp; } for(int i=max;i<=62;i++) { if(sum%i==0) { cout<<(i+1)<<endl; break; } else if(i==62) cout<<"such number is impossible!"<<endl; } } return 0; }
const member function && const static function
class 的 object 也能像 int, double 等資料型態一樣,被宣告成 const,這代表該object的資料不能被任意更動。
因此只有不會更動到資料的函式,也就是const member function能被這種const object呼叫。
宣告const member function的語法就是在()與{}間加上const:
class ID{
private:
string name;
public:
ID(string n){name=n;}
string getName() const {return name;}
};
這樣一來,如果要在getName()這個函式修改值,像是把name="unknown"的話,就會出現編譯錯誤的訊息。
另外,由於const object不能使用一般的 member function,但 const object 跟 non-const object 都能使用 const member function,在寫某些不會更動值的函式像是accessor時,可以直接加個const。
參考:
https://www.geeksforgeeks.org/const-member-functions-c/
因此只有不會更動到資料的函式,也就是const member function能被這種const object呼叫。
宣告const member function的語法就是在()與{}間加上const:
class ID{
private:
string name;
public:
ID(string n){name=n;}
string getName() const {return name;}
};
這樣一來,如果要在getName()這個函式修改值,像是把name="unknown"的話,就會出現編譯錯誤的訊息。
另外,由於const object不能使用一般的 member function,但 const object 跟 non-const object 都能使用 const member function,在寫某些不會更動值的函式像是accessor時,可以直接加個const。
參考:
https://www.geeksforgeeks.org/const-member-functions-c/
_______________________
static 通常不會單獨出現,而是配合著 const 。
宣告也是在外面,不過是 const + data type 一起。
class CScore {
protected:
//declare a static constant
static const int Max;
};
const int CScore::Max = 100; //outside class
2020年8月12日 星期三
static data members & static function
class X {
private:
static int count;
};
int X::count = 0;
static 的資料屬於所有 class object共享,就好像 class 裡的 member function一樣。又因為在定義 class 時還沒有對資料賦值,因此初始化要寫在 class 的定義外面。
class CNum {
public:
CNum(int a) { x = a; y += x; }
static void fun(CNum m) {
cout << m.x << "vs." << y << endl; }
private:
int x;
static int y;
};
int CNum::y = 0;
int main()
{
CNum O1(4), O2(7);
CNum::fun(O1);
CNum::fun(O2);
}
既然所有的object都共享同一份static 變數,要怎麼存取它呢?
可以用static的member function來存取它,如上面code。
在同一個 class 裡的member function其實本來就是共享的,加上static是為了處理 static 的資料。
private:
static int count;
};
int X::count = 0;
static 的資料屬於所有 class object共享,就好像 class 裡的 member function一樣。又因為在定義 class 時還沒有對資料賦值,因此初始化要寫在 class 的定義外面。
class CNum {
public:
CNum(int a) { x = a; y += x; }
static void fun(CNum m) {
cout << m.x << "vs." << y << endl; }
private:
int x;
static int y;
};
int CNum::y = 0;
int main()
{
CNum O1(4), O2(7);
CNum::fun(O1);
CNum::fun(O2);
}
既然所有的object都共享同一份static 變數,要怎麼存取它呢?
可以用static的member function來存取它,如上面code。
在同一個 class 裡的member function其實本來就是共享的,加上static是為了處理 static 的資料。
12019 - Doom's Day Algorithm
程式碼:
#include <iostream> using namespace std; int main() { int cases,index[12]={31,28,31,30,31,30,31,31,30,31,30,31}; string week[7]={"Saturday","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday"}; cin>>cases; while(cases--) { int m,d,total=0; cin>>m>>d; for(int i=0;i<m-1;i++) total+=index[i]; total+=d; cout<<week[(total-1)%7]<<endl; } }
11349 - Symmetric Matrix
程式碼:
#include <iostream> using namespace std; int main() { char ignore; int t; cin>>t; for(int rounds=1;rounds<=t;rounds++) { cout<<"Test #"<<rounds<<": "; int n,flag=0; long long int matrix[101][101]; cin>>ignore>>ignore>>n; for(int i=0;i<n;i++) for(int j=0;j<n;j++) cin>>matrix[i][j]; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { if(matrix[i][j]!=matrix[n-1-i][n-1-j]||matrix[i][j]<0) flag=1; } } if(flag) cout<<"Non-symmetric."<<endl; else cout<<"Symmetric."<<endl; } }
2020年8月11日 星期二
11321 - Sort! Sort!! and Sort!!!
解題心得:
1.使用內建sort()
2.負數mod出來也會是負數
3.比較時有四種狀況:不同餘、同餘皆奇、同餘皆偶、同餘一奇一偶
程式碼:
1.使用內建sort()
2.負數mod出來也會是負數
3.比較時有四種狀況:不同餘、同餘皆奇、同餘皆偶、同餘一奇一偶
程式碼:
#include <iostream> #include <algorithm> using namespace std; int n,m; bool cmp(int a,int b) { if(a%m!=b%m) return a%m<b%m; if(abs(a%2)==1&&abs(b%2)==1) return a>b; if(abs(a%2)==0&&abs(b%2)==0) return a<b; return abs(a%2); } int main() { int l[10001]; while(cin>>n>>m) { cout<<n<<" "<<m<<endl; if(n==0&&m==0) break; for(int i=0;i<n;i++) cin>>l[i]; sort(l,l+n,cmp); for(int i=0;i<n;i++) cout<<l[i]<<endl; } return 0; }
function pointer
#include <iostream> using namespace std; double f() { return 8.7; } int main() { double (*pF)(); pF=f; // or pF=&f double result=(*pF)(); cout<<result<<endl; return 0; }
執行結果:8.7
2020年8月10日 星期一
# 讀報 What Footage of the Beirut Explosion Tells Us About the Blast
https://www.nytimes.com/2020/08/05/video/beirut-explosion-footage.html
a plume of smoke:羽狀煙霧
displace:迫使...離開常居地
rip through:快速猛烈的穿透地方或建築物
envelop:環繞、包圍
confiscate from:沒收、充公
stockpile:儲存
welding:焊接
military grade:軍用級
silo:穀倉
gaping:(洞或開口)大的
Container Terminal:貨櫃中心
capsize:覆沒
detonation:引爆
mosque:清真寺
a plume of smoke:羽狀煙霧
displace:迫使...離開常居地
rip through:快速猛烈的穿透地方或建築物
envelop:環繞、包圍
confiscate from:沒收、充公
stockpile:儲存
welding:焊接
military grade:軍用級
silo:穀倉
gaping:(洞或開口)大的
Container Terminal:貨櫃中心
capsize:覆沒
detonation:引爆
mosque:清真寺
118 - Mutant Flatworld Explorers
解題心得:
用陣列紀錄如果超出範圍的前一個座標,如果那個座標曾被標記過,代表這次可以忽略。
程式碼:
用陣列紀錄如果超出範圍的前一個座標,如果那個座標曾被標記過,代表這次可以忽略。
程式碼:
#include <iostream> using namespace std; int main() { int w,h,x,y,map[51][51]={0}; char direction; cin>>w>>h; while(cin>>x>>y>>direction) { int lost=0; string command; cin>>command; for(int i=0;i<command.size();i++) { if(command[i]=='L') { switch(direction) { case 'N': direction='W';break; case 'E': direction='N';break; case 'S': direction='E';break; case 'W': direction='S';break; } } else if(command[i]=='R') { switch(direction) { case 'N': direction='E';break; case 'E': direction='S';break; case 'S': direction='W';break; case 'W': direction='N';break; } } else if(command[i]=='F') { int nextX=x,nextY=y; if(direction=='N') nextY++; else if(direction=='E') nextX++; else if(direction=='S') nextY--; else if(direction=='W') nextX--; if(nextX>w||nextX<0||nextY>h||nextY<0) { if(map[x][y]==1) continue; else { lost=1; map[x][y]=1; break; } } x=nextX,y=nextY; } } cout<<x<<" "<<y<<" "<<direction<<(lost?" LOST":"")<<endl; } return 0; }
2020年8月8日 星期六
299 - Train Swapping
程式碼:
#include <iostream> using namespace std; int main() { int n; cin>>n; while(n--) { int l,train[51],times=0; cin>>l; for(int i=0;i<l;i++) cin>>train[i]; for(int i=0;i<l;i++) { for(int j=i;j<l;j++) { if(train[i]>train[j]) { swap(train[i],train[j]); times++; } } } cout<<"Optimal train swapping takes "<<times<<" swaps."<<endl; } return 0; }
2020年8月7日 星期五
10409 - Die Game
解題心得:
可以自己手做一個小骰子模擬。
程式碼:
可以自己手做一個小骰子模擬。
程式碼:
#include <iostream> using namespace std; int main() { int num; while(cin>>num) { if(num==0) break; int top=1,n=2,w=3,bottom=7-top,s=7-n,e=7-w; while(num--) { string str; cin>>str; if(str=="north") { int temp=n; n=top; top=s; s=bottom; bottom=temp; } else if(str=="south") { int temp=s; s=top; top=n; n=bottom; bottom=temp; } else if(str=="east") { int temp=e; e=top; top=w; w=bottom; bottom=temp; } else if(str=="west") { int temp=w; w=top; top=e; e=bottom; bottom=temp; } } cout<<top<<endl; } return 0; }
10193 - All You Need Is Love
解題心得:
1.二進位轉十進位(& string to int)
2.gcd
程式碼:
1.二進位轉十進位(& string to int)
2.gcd
程式碼:
#include <iostream> #include <string> using namespace std; int to_dec(string s) { int expo=1,sum=0; for(int i=s.size()-1;i>=0;i--) { sum+=(s[i]-'0')*expo; expo*=2; } return sum; } int gcd(int a,int b) { while((a%=b)!=0&&(b%=a)!=0); return a+b; } int main() { int n; cin>>n; for(int cases=1;cases<=n;cases++) { string s1,s2; int n1,n2; cin>>s1>>s2; n1=to_dec(s1),n2=to_dec(s2); cout<<"Pair #"<<cases<<": "; if(gcd(n1,n2)!=1) cout<<"All you need is love!"<<endl; else cout<<"Love is not all you need!"<<endl; } return 0; }
2020年8月6日 星期四
copy constructor & copy assignment constructor & default constructor
copy constructor
A::A(const A& a)
{
// code
}
使用時機:有指標的時候
使用方法:
A a;
a.setValue(.....); // 初始化a
A b(a);
注意:跟Copy Assignment Operator不一樣!!
copy constructor是一種constructor,顧名思義是在初始化的時候用的。而copy assignment operator則是跟 = 有關。
copy assignment constructor
A& A::operator=(const A& a)
{
//code
return *this;
}
note:回傳參考!!
參考連結:
http://jyleef.blogspot.com/2012/04/copy-constructor.html
[C++筆記] 拷貝建構式(Copy Constructor) & 複製指派運算子(Copy Assignment Operator)
A::A(const A& a)
{
// code
}
使用時機:有指標的時候
使用方法:
A a;
a.setValue(.....); // 初始化a
A b(a);
注意:跟Copy Assignment Operator不一樣!!
copy constructor是一種constructor,顧名思義是在初始化的時候用的。而copy assignment operator則是跟 = 有關。
copy assignment constructor
A& A::operator=(const A& a)
{
//code
return *this;
}
note:回傳參考!!
參考連結:
http://jyleef.blogspot.com/2012/04/copy-constructor.html
[C++筆記] 拷貝建構式(Copy Constructor) & 複製指派運算子(Copy Assignment Operator)
default constructor
如果沒有宣告constructor的話,會自動生成一個沒有任何參數的constructor,像這樣:
如果沒有宣告constructor的話,會自動生成一個沒有任何參數的constructor,像這樣:
A::A(){;}
但如果有宣告constructor的話,就不會自動生成。
但如果有宣告constructor的話,就不會自動生成。
11005 - Cheapest Base
程式碼:
#include <iostream> #include <string> using namespace std; int main() { int cases; while(cin>>cases) { for(int rounds=1;rounds<=cases;rounds++) { cout<<"Case "<<rounds<<":"<<endl; int index[36]; for(int i=0;i<36;i++) cin>>index[i]; int num; cin>>num; while(num--) { int n,min_cost=1000000000,min_base=100; cin>>n; cout<<"Cheapest base(s) for number "<<n<<":"; for(int i=2;i<=36;i++) { int cost=0,number=n; while(number>0) { cost+=index[number%i]; number/=i; } if(cost<min_cost) min_cost=cost,min_base=i; } for(int i=2;i<=36;i++) { int cost=0,number=n; while(number>0) { cost+=index[number%i]; number/=i; } if(cost==min_cost) cout<<" "<<i; } cout<<endl; } if(rounds!=cases) cout<<endl; } } return 0; }
2020年8月5日 星期三
【Hamilton】Farmer Refuted 歌詞筆記
Refuted:駁斥
hear ye:官方發言前的開場詞,尤其在法庭上
heed:注意(警告或建議)
rabble:烏合之眾、暴民
at heart:在心裡
bloodshed:殺戮
astray:誤入歧途
unravel:破壞、拆散
have-nots:富人與窮人
straight face:故意板起的臉孔
thee:你
mange:癬
divisive:引起分歧的
indecisive:優柔寡斷的
niceties:美好、精美
hear ye:官方發言前的開場詞,尤其在法庭上
heed:注意(警告或建議)
rabble:烏合之眾、暴民
at heart:在心裡
bloodshed:殺戮
astray:誤入歧途
unravel:破壞、拆散
have-nots:富人與窮人
straight face:故意板起的臉孔
thee:你
mange:癬
divisive:引起分歧的
indecisive:優柔寡斷的
niceties:美好、精美
11063 - B2-Sequence
程式碼:
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int n,arr[101],times=1; while(cin>>n) { int flag=1; for(int i=0;i<n;i++) cin>>arr[i]; for(int i=0;i<n-1;i++) { if(arr[i]>0&&arr[i]<arr[i+1]); else flag=0; } vector<int> sq; for(int i=0;i<n;i++) { for(int j=i;j<n;j++) { if(find(sq.begin(),sq.end(),arr[i]+arr[j])!=sq.end()) flag=0; else sq.push_back(arr[i]+arr[j]); } } cout<<"Case #"<<times++<<": "; if(flag==0) cout<<"It is not a B2-Sequence."<<endl; else cout<<"It is a B2-Sequence."<<endl; cout<<endl; } return 0; }
10057 - A mid-summer night's dream.
解題心得:
好像是高中數學?
#include <iostream> #include <algorithm> using namespace std; int main() { int n,arr[1000001]; while(cin>>n) { int A,two=0,three=0; for(int i=0;i<n;i++) cin>>arr[i]; sort(arr,arr+n); if(n%2) // odd { A=arr[n/2]; for(int i=0;i<n;i++) { if(arr[i]==A) two++; } three=1; } else // even { A=arr[n/2-1]; for(int i=0;i<n;i++) { if(arr[i]==arr[n/2]||arr[i]==arr[n/2-1]) two++; } three=arr[n/2]-arr[n/2-1]+1; } cout<<A<<" "<<two<<" "<<three<<endl; } return 0; }
2020年8月4日 星期二
948 - Fibonaccimal Base
程式碼:
#include <iostream> using namespace std; int main() { int f[40],n,num; f[0]=f[1]=1; for(int i=2;i<40;i++) f[i]=f[i-1]+f[i-2]; cin>>n; while(n--) { int start=0; cin>>num; cout<<num<<" = "; for(int i=39;i>=1;i--) { if(num/f[i]) cout<<"1",start=1; else if(start) cout<<"0"; num%=f[i]; } cout<<" (fib)"<<endl; } return 0; }
10189 - Minesweeper
解題心得:
注意最後一筆測資後不需換行。
程式碼:
注意最後一筆測資後不需換行。
程式碼:
[解一]
#include <iostream> using namespace std; int main() { int n,m,times=1; while(cin>>n>>m) { if(n==0&&m==0) break; if(times!=1) cout<<endl; char map[105][105]; for(int i=0;i<100;i++) for(int j=0;j<100;j++) map[i][j]='0'; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) cin>>map[i][j]; cout<<"Field #"<<times++<<":"<<endl; for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { if(map[i][j]=='*') cout<<map[i][j]; else { int count=0; for(int x=i-1;x<=i+1;x++) { for(int y=j-1;y<=j+1;y++) { if(map[x][y]=='*') count++; } } cout<<count; } } cout<<endl; } } return 0; }
[解二]
#include <iostream> using namespace std; int main() { int n, m, counter = 1; int dx[8] = { -1,-1,-1,0,0,1,1,1 }, dy[8] = { -1,0,1,-1,1,-1,0,1 }; while (cin >> n >> m && n && m) { if (counter != 1) cout << endl; char arr[101][101]; for (int i = 0; i < 101; i++) for (int j = 0; j < 101; j++) arr[i][j] = '0'; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) cin >> arr[i][j]; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (arr[i][j] != '*') { int total = 0; for (int k = 0; k < 8; k++) { if (arr[i + dx[k]][j + dy[k]] == '*') total++; } arr[i][j] = total + '0'; } } } cout << "Field #" << counter++ << ":" << endl; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) cout << arr[i][j]; cout << endl; } } return 0; }
2020年8月3日 星期一
【Hamilton】Aaron Burr, Sir 歌詞筆記
I'm at your service:願為您效勞
sort of:有點
out of sorts:心情不佳
bursar:(學校、大學裡的)財務主管
dying wish:遺願
bargained for:預料(常與more than連用)
buy you a drink:請你喝一杯
run their mouths off:太多話
pint:品脫
Sam Adams:啤酒名
redcoats:英國軍人
chicka:語助詞
cop:警察
lock up:鎖好
corsets:束腹
brew:啤酒
prodigy:神童
verse:韻文
Good luck with that:phrase that people say when you're going to try something that they think will be hard or impossible.
imminent:即將來臨
sort of:有點
out of sorts:心情不佳
bursar:(學校、大學裡的)財務主管
dying wish:遺願
bargained for:預料(常與more than連用)
buy you a drink:請你喝一杯
run their mouths off:太多話
pint:品脫
Sam Adams:啤酒名
redcoats:英國軍人
chicka:語助詞
cop:警察
lock up:鎖好
corsets:束腹
brew:啤酒
prodigy:神童
verse:韻文
Good luck with that:phrase that people say when you're going to try something that they think will be hard or impossible.
imminent:即將來臨
d141: Linearity
解題心得:
看兩條線是否相等,就是列等式看一不一樣。
因為有除以零或者兩點相同的可能,所以把兩邊等式交叉相乘,直接看乘積是否相等。
程式碼:
看兩條線是否相等,就是列等式看一不一樣。
因為有除以零或者兩點相同的可能,所以把兩邊等式交叉相乘,直接看乘積是否相等。
程式碼:
#include<iostream> using namespace std; typedef struct { int x,y; }Point; int main() { int cases; char ignore; cin>>cases; while(cases--) { Point p1,p2,p3; cin>>p1.x>>ignore>>p1.y>>p2.x>>ignore>>p2.y>>p3.x>>ignore>>p3.y; double m1,m2; if((p2.y-p1.y)*(p3.x-p2.x)==(p2.x-p1.x)*(p3.y-p2.y)) cout<<"collinear"<<endl; else cout<<"not collinear"<<endl; } return 0; }
10268 - 498-bis
解題心得:
記得再寫一次這題。
程式碼:
記得再寫一次這題。
程式碼:
#include<iostream> using namespace std; int a[1000000]; int derivative(int x,int max) { long long sum=0,exp=1; int i; for(i=max-1;i>=0;i--) { sum+=a[i]*exp*(max-i); exp*=x; } return sum; } int main() { int x,n; while(cin>>x) { for(n=0;;n++) { cin>>a[n]; if(getchar()=='\n') break; } cout<<derivative(x,n)<<endl; } return 0; }
2020年8月2日 星期日
簡易翻牌遊戲
使用說明:
總共36格,內為A~R x2 隨機排列的表格。
索引從0開始,35結束。
依指示輸入想翻開的卡牌,若該次兩張牌相同,則成功找出;反之則蓋回去。
沒有防呆,不能輸入0~35外的數字、已被翻開的卡牌索引、每次不得輸入兩相同索引。
(也許還有其他錯誤?)
程式碼:
總共36格,內為A~R x2 隨機排列的表格。
索引從0開始,35結束。
依指示輸入想翻開的卡牌,若該次兩張牌相同,則成功找出;反之則蓋回去。
沒有防呆,不能輸入0~35外的數字、已被翻開的卡牌索引、每次不得輸入兩相同索引。
(也許還有其他錯誤?)
程式碼:
#include <iostream> using namespace std; int main() { srand(time(NULL)); int index[18]={0},counter=0; char map[6][6]={'*'},alpha[6][6]; for(int i=0;i<6;i++) for(int j=0;j<6;j++) map[i][j]='*',alpha[i][j]='*'; for(int i=0;i<6;i++) { for(int j=0;j<6;j++) { int letter=rand()%18; while(index[letter]>=2) letter=rand()%18; alpha[i][j]=char(letter+'A'); index[letter]++; } } for(int i=0;i<6;i++) { for(int j=0;j<6;j++) cout<<alpha[i][j]<<" "; cout<<endl; } while(counter<36) { int cardIndex1,cardIndex2; cout<<"Please enter card index: "; cin>>cardIndex1; for(int i=0;i<6;i++) { for(int j=0;j<6;j++) { if(cardIndex1/6==i&&cardIndex1%6==j) cout<<alpha[i][j]<<" "; else cout<<map[i][j]<<" "; } cout<<endl; } cout<<"Please enter card index: "; cin>>cardIndex2; for(int i=0;i<6;i++) { for(int j=0;j<6;j++) { if(cardIndex1/6==i&&cardIndex1%6==j) cout<<alpha[i][j]<<" "; else if(cardIndex2/6==i&&cardIndex2%6==j) cout<<alpha[i][j]<<" "; else cout<<map[i][j]<<" "; } cout<<endl; } if(alpha[cardIndex1/6][cardIndex1%6]==alpha[cardIndex2/6][cardIndex2%6]) { cout<<"Good Job!"<<endl; counter+=2; map[cardIndex1/6][cardIndex1%6]=alpha[cardIndex1/6][cardIndex1%6]; map[cardIndex2/6][cardIndex2%6]=alpha[cardIndex2/6][cardIndex2%6]; } else cout<<"Try Again!"<<endl; } cout<<"Congratulation!!"<<endl; }
2020年8月1日 星期六
10908 - Largest Square
解題心得:
細節要注意。
程式碼:
細節要注意。
程式碼:
#include <iostream> using namespace std; int main() { int t,m,n,q,r,c; cin>>t; while(t--) { char map[100][100]={'0'}; cin>>m>>n>>q; cout<<m<<" "<<n<<" "<<q<<endl; for(int i=0;i<m;i++) for(int j=0;j<n;j++) cin>>map[i][j]; while(q--) { cin>>r>>c; int length=0,out=0; char target=map[r][c]; while(1) { int i,j; for(i=r-length;i<=r+length;i++) { for(j=c-length;j<=c+length;j++) { if(map[i][j]!=target) { out=1; break; } else if(i<0||j<0||i>=m||j>=n) { out=1; break; } } if(out) break; } if(out) { length--; break; } length++; } cout<<length*2+1<<endl; } } return 0; }
訂閱:
文章 (Atom)