<h1>Mark 解題紀錄</h1> <h3>CPE一星必考題</h3> 1.<h3>1.Vito's Family </h3> <p>題目大意:求出黑幫老大家到所有的親戚的家的距離的和為最小。</p> ```c++= #include <bits/stdc++.h> using namespace std; int main() { int t=0; int a[500]={0}; cin>>t; while(t--){ int s=0; cin>>s; for(int i=0;i<s;i++){ cin>>a[i]; } sort(a,a+s); int mid=a[s/2]; int sum=0; for(int i=0;i<s;i++){ sum+=abs(mid-a[i]); } cout<<sum<<endl; } } ``` --- <h3>2.Hashmat the brave warrior </h3> <p>題目大意:計算己方與敵方士兵的數目差距。</p> ```c++= #include <bits/stdc++.h> using namespace std; int main() { long long a,b; while(cin>>a>>b){ if(a>b){ swap(a,b); } cout<<b-a<<endl; } } ``` --- <h3>3.Primary Arithmetic </h3> <p>題目大意:由右至左一位一位相加。如果相加的結果大於等於10就有進位(carry)的情況出現。</p> ```c++= #include <iostream> using namespace std; int main() { long long a,b; while(cin>>a>>b){ int count=0; int e=0; if(a==0 && b==0){ break; }else{ while(a!=0 || b!=0){ int c=a%10; int d=b%10; if(c+d+e>=10){ count++; e=1; }else{ e=0; } a=a/10; b=b/10; } if(count==1){ cout<<"1 carry operation."<<endl; }else if(count==0){ cout<<"No carry operation."<<endl; }else{ cout<<count<<" carry operations."<<endl; } } } } ``` --- <h3>4.The 3n + 1 problem </h3> <p>題目大意:輸入n,如果 n = 1 結束,如果 n 是奇數那麼n=3*n+1,此數列的長度稱為n的cycle-length。上面提到的例子, 22 的 cycle length為 16,問題來了:對任2個整數i,j我們想要知道介於i,j(包含i,j)之間的數所產生的數列中最大的 cycle length 是多少。</p> ```c++= #include <bits/stdc++.h> using namespace std; int len(int i){ int lengs=0; for(lengs=1;i!=1;lengs++){ if(i%2==0){ i/=2; }else{ i=3*i+1; } } return lengs; } int main() { long long a,b; while(cin>>a>>b){ int max=0; int start=0,end=0; if(a>b){ start=b; end=a; }else{ start=a; end=b; } for(int i=start;i<=end;i++){ if(len(i)>max){ max=len(i); } } cout<<a<<" "<<b<<" "<<max<<endl; } } ``` --- <h3>5. You can say 11 </h3> <p>題目大意:給你一個正整數 N,判定它是否是 11 的倍數。</p> ```c++= #include <bits/stdc++.h> using namespace std; int main() { string s; while(cin>>s){ int odd=0,two=0; int sum=0; if(s=="0"){ break; }else{ for(int i=0;i<s.length();i+=2){ odd+=s[i]-'0'; } for(int i=1;i<s.length();i+=2){ two+=s[i]-'0'; } sum=abs(odd-two); } if(sum%11==0){ cout<<s<<" is a multiple of 11."<<endl; }else{ cout<<s<<" is not a multiple of 11."<<endl; } } } ``` --- <h3>6. Bangla Numbers </h3> <p>題目大意:???</p> ```c++= #include <bits/stdc++.h> using namespace std; void number(long long int a){ if(a>=10000000){ number(a/10000000); cout<<" kuti"; a%=10000000; } if(a>=100000){ number(a/100000); cout<<" lakh"; a%=100000; } if(a>=1000){ number(a/1000); cout<<" hajar"; a%=1000; } if(a>=100){ number(a/100); cout<<" shata"; a%=100; } if(a){ cout<<" "<<a; } } int main() { long long int a; int count=1; while(cin>>a){ printf("%4d.",count); if(a==0)cout<<" 0"; number(a); cout<<endl; count++; } } ``` --- <h3>7. List of Conquests </h3> <p>題目大意:????。</p> ```c++= #include <bits/stdc++.h> using namespace std; int main() { int a; cin>>a; string s; string country[a]; for(int i=0;i<a;i++){ cin>>country[i]; getline(cin,s); } sort(country,country+a); for (int i=0;i<a;){ int j=0; cout << country[i] << " "; int count = 1; for (j=i+1;j<a;j++){ if (country[i] != country[j]){ break; } count++; } i = j; cout << count << endl; } } ``` --- <h3>8. What's Cryptanalysis? </h3> <p>題目大意:輸入一段文字,分析其 ASCII 與次數,單測資,輸入的第一個數字 n 代表接下來的輸入行數,每行輸入可能含有多個字元 (包含空白),輸入的字母大小寫視為同一字元,按照規則輸出 字母 (大寫) & 次數,按照次數由大到小,同樣次數按照 ASCII 由小到大</p> ```c++= #include <bits/stdc++.h> using namespace std; int main() { int a; cin>>a; string s; string country[a]; for(int i=0;i<a;i++){ cin>>country[i]; getline(cin,s); } sort(country,country+a); for (int i=0;i<a;){ int j=0; cout << country[i] << " "; int count = 1; for (j=i+1;j<a;j++){ if (country[i] != country[j]){ break; } count++; } i = j; cout << count << endl; } } ``` --- <h3>9.Decode the Mad man </h3> <p>題目大意:解碼,請看你鍵盤會發現規律是往減兩格就是答案</p> ```c++= #include <bits/stdc++.h> using namespace std; int main() { string a="`1234567890-=qwertyuiop[]\\asdfghjkl;'zxcvbnm,./"; string s; while(getline(cin,s)){ for(int i=0;i<s.length();i++){ if(s[i]<=90 && s[i]>=65){ s[i]+=32; } } for(int i=0;i<s.length();i++){ if(s[i]==' '){ cout<<' '; } for(int j=0;j<a.length();j++){ if(s[i]==a[j]){ cout<<a[j-2]; } } } cout<<endl; } } ``` --- <h3>10. Summing Digits</h3> <p>題目大意:?????</p> ```c++= #include <bits/stdc++.h> using namespace std; int count,b,n; int number(int n){ b=0; if(n>9){ while(n>0){ b+=n%10; n/=10; } return number(b); }else{ return n; } } int main() { long long a; while(cin>>a){ if(a==0){ break; }else{ cout<<number(a)<<endl; } } } ``` --- <h3>11. Common Permutation </h3> <p>題目大意:給定兩個由小寫字母組成的字串a和b。印出最長的小寫字串x,使得x經過重新排列後為a的子序列,且x經過重新排列後為b的子序列。</p> ```c++= #include <bits/stdc++.h> using namespace std; int main() { string s,t; while(getline(cin,s)&&getline(cin,t)){ vector <char> a; for(int i=0;i<s.length();i++){ for(int j=0;j<t.length();j++){ if(s[i]==t[j]){ a.push_back(s[i]); t[j]='1'; break; } } } sort(a.begin(),a.end()); for(auto& i:a){ cout<<i; } cout<<endl; } } ``` --- <h3>12. Rotating Sentences </h3> <p>題目大意:將數列文字往順時針方向旋轉90度。也就是說將原本由左到右,由上到下的句子輸出成由上到下,由右到左。</p> ```c++= #include <bits/stdc++.h> using namespace std; int main(){ string s[101]; int coun=0,max=0; while (getline(cin, s[coun])) { if (s[coun].length()>max){ max=s[coun].length(); } coun++; } for(int i=0;i<max;i++){ for(int j=coun-1;j>=0;j--){ if(s[j].length()>i){ cout<<s[j][i]; }else{ cout<<" "; } } cout<<endl; } } ``` --- <h3>13. TEX Quotes </h3> <p>題目大意:每一組雙引號的第一個 " 必須用``來代替,每一組雙引號的第二個 " 必須用''來代替。</p> ```c++= #include <bits/stdc++.h> using namespace std; int main() { string s; int count=1; while(getline(cin,s)){ for(int i=0;i<s.length();i++){ if(s[i]=='"'){ if(count%2!=0){ cout<<"``"; count=2; }else{ cout<<"''"; count=1; } }else{ cout<<s[i]; } } cout<<endl; } } ``` --- <h3>14. Doom's Day Algorithm</h3> <p>題目大意:給你月份跟日期,求出那天是星期幾。</p> ```c++= #include <bits/stdc++.h> using namespace std; int main() { int t; cin>>t; int Day[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; while(t--){ int a,b,Today=0; cin>>a>>b; int days=0; for(int i=1;i<a;i++){ days+=Day[i]; } Today=(days+b)%7; if(Today==1){ cout<<"Saturday"<<endl; }else if(Today==2){ cout<<"Sunday"<<endl; }else if(Today==3){ cout<<"Monday"<<endl; }else if(Today==4){ cout<<"Tuesday"<<endl; }else if(Today==5){ cout<<"Wednesday"<<endl; }else if(Today==6){ cout<<"Thursday"<<endl; }else if(Today==0){ cout<<"Friday"<<endl; } } } ``` --- <h3>15. Jolly Jumpers</h3> <p>題目大意:有n個整數的序列我們稱為jolly jumper,如果相鄰的2個數其差的絕對值恰好為1到n-1。</p> ```c++= #include <bits/stdc++.h> using namespace std; int main(){ int a,b; while(cin>>a){ int count=0; vector <int> box; vector <int> box2; for(int i=0;i<a;i++){ cin>>b; box.push_back(b); } for(int i=0;i<a-1;i++){ box2.push_back(abs(box[i]-box[i+1])); } sort(box2.begin(),box2.end()); int Count=0; for(int i=0;i<a-1;i++){ if(box2[i]!=i+1){ Count=1; break; } } if(Count){ cout<<"Not jolly"<<endl; }else{ cout<<"Jolly"<<endl; } } } ``` --- <h3>16. What is the Probability ?</h3> <p>題目大意:那玩家play的順序是從第一個到最後一個,若沒有人獲勝,就到第二輪,若第二輪還沒有人獲勝,就到第三輪,直到有人獲勝為止。算法大致上就是先求出第一輪x玩家獲勝的機率,再加上第二輪x玩家獲勝的機率...一直加到無窮大。</p> ```c++= #include <bits/stdc++.h> using namespace std; int main() { int t,a,b; double p,q,s; cin>>t; while(t--){ cin>>a>>p>>b; q=1-p; if(p==0) cout<<"0.0000"<<endl; else{ s=pow(q,b-1)*p/(1-pow(q,a)); printf("%.4f\n",s); } } } ``` --- <h3>17. The Hotel with Infinite Rooms</h3> <p>題目大意:如果一組四個成員的旅行團在8月1日早晨到達,則它將在8月4日晚上離開酒店下一組五個成員將在8月5日早晨入住並且停留五天,依此類推。現在給定第一組旅行團人數,您必須回答在指定日期入住的旅行團人數。</p> ```c++= #include <bits/stdc++.h> using namespace std; int main() { long long s,d; while(cin>>s>>d){ while(d>0){ d-=s; if(d<=0){ break; }else{ s++; } } cout<<s<<endl; } } ``` --- <h3>18. 498-bis</h3> <p>題目大意:給你一組方程式和X求微分後的值。</p> ```c++= #include <bits/stdc++.h> using namespace std; int main() { int t; string s; while(cin>>t){ int sum=0; vector <int> v; getline(cin,s); getline(cin,s); stringstream ss(s); int pw=1; while(ss>>s){ v.push_back(stoi(s)); } for(int i=v.size()-2;i>=0;i--){ sum+=v[i]*(v.size()-1-i)*pw; pw*=t; } cout<<sum<<endl; } } ``` --- <h3>19. Odd Sum</h3> <p>題目大意:給你一個範圍 a 到 b ,請你找出 a 與 b 之間所有奇數的和。</p> ```c++= #include <bits/stdc++.h> using namespace std; int main() { int a; int b=0,c=0; int count=0; cin>>a; while(a--){ count++; cin>>b>>c; int sum=0; for(int i=b;i<=c;i++){ if(i%2!=0){ sum+=i; } } cout<<"Case "<<count<<": "<<sum<<endl; } } ``` --- <h3>20. Beat the Spread!</h3> <p>題目大意:?????</p> ```c++= #include <bits/stdc++.h> using namespace std; int main() { int t,a,b; cin>>t; while(t--){ cin>>a>>b; if(b>a || (a+b)%2){ cout<<"impossible"<<endl; }else{ cout<<(a+b)/2<<" "<<(a-b)/2<<endl; } } } ``` --- <h3>21. Symmetric Matrix</h3> <p>題目大意:?????</p> ```c++= #include <bits/stdc++.h> using namespace std; int main() { } ``` --- <h3>22. Square Numbers</h3> <p>題目大意:求出 a 與 b 之間 (含) 有幾個完全平方數。</p> ```c++= #include <bits/stdc++.h> using namespace std; int main() { int a,b,sq; while(cin>>a>>b && a!=0 && b!=0){ int count=0; for(int i=a;i<=b;i++){ sq=sqrt(i); if(sq*sq==i){ count++; } } cout<<count<<endl; } } ``` --- <h3>23. B2-Sequence</h3> <p>題目大意:「B2數列」係指一正整數數列 1<= b1 < b2 < b3 ...,其中所有的 bi + bj (i <= j)皆不相等。</p> ```c++= #include <bits/stdc++.h> using namespace std; int main() { int t; int count=0; int a[t]={0}; while(cin>>t){ count++; bool Hello=true; for(int i=0;i<t;i++){ cin>>a[i]; if(a[i]<1){ Hello=false; } } for(int i=0;i<t-1;i++){ if(a[i]>a[i+1]){ Hello=false; } } int sum=0; set <int> box; box.clear(); if(Hello){ for(int i=0;i<t;i++){ for(int j=i;j<t;j++){ box.insert(a[i]+a[j]); } } for(int i=0;i<=t;i++){ sum+=i; } if(box.size()==sum){ cout<<"Case #"<<count<<": It is a B2-Sequence."<<endl; cout<<endl; }else{ cout<<"Case #"<<count<<": It is not a B2-Sequence."<<endl; cout<<endl; } }else{ cout<<"Case #"<<count<<": It is not a B2-Sequence."<<endl; cout<<endl; } } } ``` --- <h3>24. Back to High School Physics</h3> <p>題目大意:求 2t 秒後所經過的位移是多少</p> ```c++= #include <bits/stdc++.h> using namespace std; int main() { int a,b; while(cin>>a>>b){ cout<<2*a*b<<endl; } } ``` --- <h3>25. An Easy Problem!</h3> <p>題目大意:???</p> ```c++= #include <bits/stdc++.h> using namespace std; int main() { string s; while(getline(cin,s)){ int sum=0,max=1; int test; for(int i=0;i<s.length();i++){ if(s[i]>=48 && s[i]<=122){ if(s[i]>='0' && s[i]<='9'){ test=s[i]-48; }else if(s[i]>='A' && s[i]<='Z'){ test=s[i]-55; }else if(s[i]>='a' && s[i]<='z'){ test=s[i]-61; } sum+=test; if(test>max){ max=test; } }else{ continue; } } int i=0; for(i=max;i<62;i++){ if(sum%i==0){ cout<<i+1<<endl; break; } } if(i==62){ cout<<"such number is impossible!"<<endl; } } } ``` --- <h3>26. Fibonaccimal Base</h3> <p>題目大意:給你一個十進位將他轉為費氏數列進制輸出。</p> ```c++= #include <bits/stdc++.h> using namespace std; int main() { int FB[39]={0,1,1}; for(int i=3;i<=40;i++){ FB[i]=FB[i-1]+FB[i-2]; } int t,a; cin>>t; while(t--){ vector <int> v; cin>>a; int b=a; for(int i=39;i>1;i--){ if(a>=FB[i]){ a-=FB[i]; v.push_back(1); }else{ v.push_back(0); } } cout<<b<<" = "; for(int i=0;i<=39;i++){ if(v[i]==1){ for(int j=i;j<=37;j++){ cout<<v[j]; } break; } } cout<<" (fib)"<<endl; } } ``` --- <h3>27. Funny Encryption Method</h3> <p>題目大意:將十進制轉為二進制,算有幾個1,再將數字從十六進制轉為二進制,算有幾個1。</p> ```c++= #include <bits/stdc++.h> using namespace std; int main(){ int t,a, b, c, d; cin >> t; while(t--){ int n=0,n1=0; cin>>a; int e=a; while(a > 0){ b=a%2; if(b==1){ n++; } a/=2; } while(e>0){ c=e%10; while(c>0){ d=c%2; if(d==1){ n1++; } c/=2; } e/=10; } cout<<n<< " " <<n1<<endl; } } ``` --- <h3>28. Parity</h3> <p>題目大意:將十進制轉為二進制,算有幾個1。</p> ```c++= #include <bits/stdc++.h> using namespace std; int main() { int a; while(cin>>a && a){ int count=0; vector <int> v; while(a>0){ if(a%2==0){ v.push_back(0); }else{ count++; v.push_back(1); } a/=2; } cout<<"The parity of "; reverse(v.begin(),v.end()); for(auto&i:v){ cout<<i; } cout<<" is "<<count<<" (mod 2)."<<endl; } } ``` --- <h3>29. Cheapest Base</h3> <p>題目大意:將十進制轉為二進制,算有幾個1。</p> ```c++= #include <bits/stdc++.h> using namespace std; int main() { int t,n,b; int coun=0; cin>>t; while(t--){ coun++; int cost[36]={0}; for(int i=0;i<36;i++){ cin>>cost[i]; } cin>>n; cout<<"Case "<<coun<<":"<<endl; while(n--){ cin>>b; int min=0,c,d; int cheast[37]={0}; for(int i=2;i<=36;i++){ c=b; int sum=0; while(c>0){ d=c%i; sum+=cost[d]; c/=i; } if(sum<=min || min==0) { cheast[i]=sum; min=sum; } } cout<<"Cheapest base(s) for number "<<b<<":"; for(int i=2;i<=36;i++){ if(cheast[i]==min){ cout<<" "<<i; } } cout<<endl; if(n==0 &&t!=0){ cout<<endl; } } } } ``` --- <h3>30. Hartals</h3> <p>題目大意:?????</p> ```c++= #include <bits/stdc++.h> using namespace std; int main() { int t,p,n; cin>>t; while(t--){ int a; vector <int> v; cin>>p; cin>>n; for(int i=0;i<n;i++){ cin>>a; v.push_back(a); } int h=0; for(int i=0;i<=p;i++){ if(i%7==6 || i%7==0){ continue; }else{ for(int j=0;j<n;j++){ if(i%v[j]==0){ h++; break; } } } } cout<<h<<endl; } } ``` --- <h3>31. All You Need Is Love</h3> <p>題目大意:看給的兩個二進位是不是互質</p> ```c++= #include <bits/stdc++.h> using namespace std; int main() { int t; string s,s1; cin>>t; int count=1; while(t--){ cin>>s>>s1; int sum=0; int sum2=0; for(int i=0;i<s.length();i++){ if(s[i]=='1'){ sum+=pow(2,s.length()-i-1); } } for(int i=0;i<s1.length();i++){ if(s1[i]=='1'){ sum2+=pow(2,s1.length()-i-1); } } if(__gcd(sum,sum2)!=1){ cout<<"Pair #"<<count<<": All you need is love!"<<endl; }else{ cout<<"Pair #"<<count<<": Love is not all you need!"<<endl; } count++; } } ``` --- <h3>32. Divide, But Not Quite Conquer!</h3> <p>題目大意: 1. a[1] = n, a[i] = a[i − 1] ÷ m, for all 1 < i ≤ k 2. a[i] 被 m 整除(a[i] mod m = 0) for all 1 ≤ i < k 3. a[1] > a[2] > a[3] > ... > a[k] </p> ```c++= #include <bits/stdc++.h> using namespace std; int main() { long long s,d; while(cin>>s>>d){ if(d<=1 || s<d){ cout<<"Boring!"<<endl; continue; } vector <long long int> box; while(s){ box.push_back(s); s/=d; } bool hello=true; for(int i=0;i<box.size();i++){ if(i==box.size()-1){ break; } if(box[i]%d!=0){ hello=false; } } for(int i=0;i<box.size()-1;i++){ if(box[i]<box[i+1]){ hello=false; } } if(box.back()!=1){ hello=false; } if(hello){ for(int i = 0;i < box.size();i++){ cout<<box[i]; if(i!=box.size()-1){ cout<<" "; } } cout<<endl; }else{ cout<<"Boring!"<<endl; } } } ``` --- <h3>33. Simply Emirp</h3> <p>題目大意:看是不是互質,如果是互質在看倒數是不是互質。</p> ```c++= #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int a; while(cin>>a){ int count=1; int b=0,c=0,re=0; int d=a; for(int i=2;i<a;i++){ if(a%i==0){ count=0; break; } } if(count==1){ while(a!=0){ b=a%10; re=re*10+b; a/=10; } if(re!=d){ count=2; for(int i=2;i<re;i++){ if(re%i==0){ count=1; break; } } } } if(count==2){ cout<<d<<" is emirp."<<endl; }else if(count==1){ cout<<d<<" is prime."<<endl; }else{ cout<<d<<" is not prime."<<endl; } } } ``` --- <h3>34. 2 the 9s</h3> <p>題目大意:給你一個正整數N,判斷他是不是9的倍數,而且如果他是9的倍數你還需要判斷它的 9-degree。</p> ```c++= #include <bits/stdc++.h> using namespace std; int count; int number(long long int n){ if(n % 9) return 0; else if(n == 9){ return ++count; } int i = 0; while(n){ i += n % 10; n /= 10; } count++; return number(i); } int main() { string s; while(cin>>s && s !="0"){ int sum=0; for(int i=0;i<s.length();i++){ sum+=s[i]-'0'; } count = 0; count = number(sum); if(count!=0){ cout<<s<<" is a multiple of 9 and has 9-degree "<<count <<".\n"; }else{ cout<<s<<" is not a multiple of 9.\n"; } } } ``` --- <h3>35. GCD</h3> <p>題目大意:找出因數。</p> ```c++= #include <bits/stdc++.h> using namespace std; int main() { int t; while(cin>>t &&t){ int G=0; for(int i=1;i<t;i++){ for(int j=i+1;j<=t;j++){ G+=__gcd(i,j); } } cout<<G<<endl; } return 0; } ``` --- <h3>36. Largest Square</h3> <p>題目大意:==。</p> ```c++= 我不會寫 ``` --- <h3>37. Satellites</h3> <p>題目大意:==。</p> ```c++= #include <bits/stdc++.h> using namespace std; int main() { double s,a; string b; while(cin>>s>>a>>b){ double pi=3.14159265359/180; if(b[0]=='m'){ a/=60; } while(a>=360){ a-=360; } while(a>180){ abs(a-=360); } printf("%.6f %.6f\n",abs((s+6440)*a*pi),abs((s+6440)*sin(a/2*pi)*2)); } } ``` --- <h3>38. Can You Solve It?</h3> <p>題目大意:花費的最小步數。</p> ```c++= #include <bits/stdc++.h> using namespace std; int number(int x,int y){ return (x+y)*(x+y+1)/2+x; } int main() { int n,x1,y1,x2,y2; int coun=0; cin>>n; while(n--){ coun++; cin>>x1>>y1>>x2>>y2; int count=0; cout<<"Case "<<coun<<": "<<number(x2,y2)-number(x1,y1)<<endl; } } ``` --- <h3>39. Fourth Point !!</h3> <p>題目大意:給定平行四邊形的兩個相鄰邊的端點的 (x, y)座標,找到第四個點的 (x, y)座標。</p> ```c++= #include <bits/stdc++.h> using namespace std; int main() { double x[4],y[4],ax,ay; while(cin>>x[0]>>y[0]){ ax=x[0]; ay=y[0]; for(int i=1;i<4;i++){ cin>>x[i]>>y[i]; ax+=x[i]; ay+=y[i]; } for(int i=0;i<4;i++){ for(int j=i+1;j<4;j++){ if(x[i]==x[j] && y[i]==y[j]){ cout<<fixed<<setprecision(3)<<ax-3*x[i]<<' '<<ay-3*y[i]<<endl; } } } } } ``` --- <h3>40. A mid-summer night's dream.</h3> <p>題目大意:????。</p> ```c++= #include <bits/stdc++.h> using namespace std; int main() { int n; int a; while(cin>>n){ vector <int> v; for(int i=0;i<n;i++){ cin>>a; v.push_back(a); } sort(v.begin(),v.end()); int mid=0,mid2=0; if(n%2==0){ mid=v[n/2-1]; mid2=v[n/2]; }else{ mid=v[n/2]; mid2=mid; } int coun=0; for(int i=0;i<v.size();i++){ if(v[i]==mid || v[i]==mid2){ coun++; } } cout<<mid<<" "<<coun<<" "<<(mid2-mid)+1<<endl; } } ``` --- <h3>41. Tell me the frequencies!</h3> <p>題目大意:給你一列文字,請你找出各字元出現的次數。</p> ```c++= #include <bits/stdc++.h> using namespace std; int main() { string s; int coun=0; while(getline(cin,s)){ if(coun++){ cout<<endl; } int count[200]={0}; for(int i=0;i<s.length();i++){ count[s[i]]++; } for(int i=1;i<=s.length();i++){ for(int j=127;j>= 32;j--){ if(count[j]==i){ cout<<j<<" "<<i<<endl; } } } } } ``` --- <h3>42. Train Swapping</h3> <p>題目大意:計算最少需要交換幾次兩個相鄰車廂,才能將所有車廂依序排好。</p> ```c++= #include <bits/stdc++.h> using namespace std; int main() { int t; cin>>t; while(t--){ int n=0,count=0; cin>>n; int a[50]={0}; for(int i=0;i<n;i++){ cin>>a[i]; } for(int i=0;i<n-1;i++){ for(int j=0;j<n-1-i;j++){ if(a[j]>a[j+1]){ int temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; count++; } } } cout<<"Optimal train swapping takes "<<count<<" swaps."<<endl; } } ``` --- <h3>43. Hardwood Species</h3> <p>題目大意:給你種子名稱,算出種子占幾趴</p> ```c++= #include <bits/stdc++.h> using namespace std; int main() { int t; string s,a; cin>>t; getline(cin, a); getline(cin, a); while(t--){ double sum=0; map <string , int> m; while(getline(cin,s) && !s.empty()){ m[s]++; sum++; } for(auto&i:m){ cout<<i.first<<" "<<fixed<<setprecision(4)<<(i.second/sum)*100<<endl; } if(t>0){ cout<<endl; } } } ``` --- <h3>44. Minesweeper</h3> <p>題目大意:算那九宮格周遭出現幾次地雷。</p> ```c++= #include <bits/stdc++.h> using namespace std; int main() { int a,b,qq=0; int count=0; while(cin>>a>>b && (a || b)){ if(qq++){ cout<<endl; } char c; count++; int d[102][102]={0}; for(int i=1;i<=a;i++){ for(int j=1;j<=b;j++){ cin>>c; if(c=='*'){ d[i][j]-=10; for(int x=i-1;x<=i+1;x++){ for(int y=j-1;y<=j+1;y++){ d[x][y]++; } } } } } cout<<"Field #"<<count<<":"<<endl; for(int i=1;i<=a;i++){ for(int j=1;j<=b;j++){ if(d[i][j]<0){ cout<<'*'; }else{ cout<<d[i][j]; } } cout<<endl; } } } ``` --- <h3>45. Die Game</h3> <p>題目大意:告訴你方向,輸出點數。</p> ```c++= #include <bits/stdc++.h> using namespace std; int main() { map <string ,int> mp; int a,temp; string s; while(cin>>a && a){ mp["top"]=1; mp["north"]=2; mp["west"]=3; mp["east"]=4; mp["south"]=5; mp["bottom"]=6; while(a--){ cin>>s; if(s=="north"){ temp=mp["north"]; mp["north"]=mp["top"]; mp["top"]=mp["south"]; mp["south"]=mp["bottom"]; mp["bottom"]=temp; }else if(s=="east"){ temp=mp["east"]; mp["east"]=mp["top"]; mp["top"]=mp["west"]; mp["west"]=mp["bottom"]; mp["bottom"]=temp; }else if(s=="south"){ temp=mp["south"]; mp["south"]=mp["top"]; mp["top"]=mp["north"]; mp["north"]=mp["bottom"]; mp["bottom"]=temp; }else if(s=="west"){ temp=mp["west"]; mp["west"]=mp["top"]; mp["top"]=mp["east"]; mp["east"]=mp["bottom"]; mp["bottom"]=temp; } } cout<<mp["top"]<<endl; } } ``` --- <h3>46. Eb Alto Saxophone Player</h3> <p>題目大意:計算每一根手指頭按了多少次按鍵,如果某一按鍵在下一音符時不會用到,那麼就會放開,否則就是維持按著的情況。</p> ```c++= #include <bits/stdc++.h> using namespace std; int main() { map <char, vector<int>> mp; mp['a']={0,1,1,0,0,0,0,0,0,0}; mp['b']={0,1,0,0,0,0,0,0,0,0}; mp['c']={0,1,1,1,0,0,1,1,1,1}; mp['d']={0,1,1,1,0,0,1,1,1,0}; mp['e']={0,1,1,1,0,0,1,1,0,0}; mp['f']={0,1,1,1,0,0,1,0,0,0}; mp['g']={0,1,1,1,0,0,0,0,0,0}; mp['A']={1,1,1,0,0,0,0,0,0,0}; mp['B']={1,1,0,0,0,0,0,0,0,0}; mp['C']={0,0,1,0,0,0,0,0,0,0}, mp['D']={1,1,1,1,0,0,1,1,1,0}; mp['E']={1,1,1,1,0,0,1,1,0,0}; mp['F']={1,1,1,1,0,0,1,0,0,0}; mp['G']={1,1,1,1,0,0,0,0,0,0}; int t; string s; cin>>t; getline(cin,s); while(t--){ getline(cin,s); int ans[11]={0}; int last[11]={0}; for(int i=0;i<s.length();i++){ for(int j=0;j<10;j++){ if(mp[s[i]][j]){ if (last[j]){ continue; } else { last[j] = 1; ans[j]++; } }else{ last[j]=0; } } } for(int i=0;i<10;i++){ cout<<ans[i]; if(i!=9){ cout<<" "; } } cout<<endl; } } ``` --- <h3>47. Mutant Flatworld Explorers</h3> <p>題目大意:計算每一根手指頭按了多少次按鍵,如果某一按鍵在下一音符時不會用到,那麼就會放開,否則就是維持按著的情況。</p> ```c++= #include <bits/stdc++.h> using namespace std; int main() { map <char, vector<int>> mp; mp['a']={0,1,1,0,0,0,0,0,0,0}; mp['b']={0,1,0,0,0,0,0,0,0,0}; mp['c']={0,1,1,1,0,0,1,1,1,1}; mp['d']={0,1,1,1,0,0,1,1,1,0}; mp['e']={0,1,1,1,0,0,1,1,0,0}; mp['f']={0,1,1,1,0,0,1,0,0,0}; mp['g']={0,1,1,1,0,0,0,0,0,0}; mp['A']={1,1,1,0,0,0,0,0,0,0}; mp['B']={1,1,0,0,0,0,0,0,0,0}; mp['C']={0,0,1,0,0,0,0,0,0,0}, mp['D']={1,1,1,1,0,0,1,1,1,0}; mp['E']={1,1,1,1,0,0,1,1,0,0}; mp['F']={1,1,1,1,0,0,1,0,0,0}; mp['G']={1,1,1,1,0,0,0,0,0,0}; int t; string s; cin>>t; getline(cin,s); while(t--){ getline(cin,s); int ans[11]={0}; int last[11]={0}; for(int i=0;i<s.length();i++){ for(int j=0;j<10;j++){ if(mp[s[i]][j]){ if (last[j]){ continue; } else { last[j] = 1; ans[j]++; } }else{ last[j]=0; } } } for(int i=0;i<10;i++){ cout<<ans[i]; if(i!=9){ cout<<" "; } } cout<<endl; } } ``` --- <h3>48. Cola</h3> <p>題目大意:3瓶空可樂罐換一瓶可樂。</p> ```c++= #include <bits/stdc++.h> using namespace std; int main() { int a; while(cin>>a){ cout<<(int)(1.5*a)<<endl; } } ``` --- <h3>49. Sort! Sort!! and Sort!!!</h3> <p>題目大意:先利用每個數字除以M的餘數由小到大排,若排序中比較的兩數為一奇一偶且兩數除以M 的餘數相等,則奇數要排在偶數前面。若兩奇數除以M餘數大小相等,則原本數值較大的奇數排在前面。同樣的,若兩偶數除以M餘數大小相等,則較小的偶數排在前面。至於負數的餘數計算和 C 語言裡的定義相同,即負數的餘數絕對不會大於零。</p> ```c++= #include <bits/stdc++.h> using namespace std; int n,m; bool exe(int x,int y){ if((x%m) != (y%m)){ return (x%m)<(y%m); }else{ if((x%2!=0) && (y%2!=0)){ return x>y; }else if((x%2==0) && (y%2==0)){ return x<y; }else{ return(x%2); } } } int main() { while(cin>>n>>m){ cout<<n<<" "<<m<<endl; vector <int> s(n); for(auto& i:s){ cin>>i; } sort(s.begin(), s.end(), exe); for(auto& i:s){ cout<<i<<endl; } } } ``` ---