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 的資料。
2020年8月12日 星期三
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的話,就不會自動生成。
訂閱:
文章 (Atom)