# C++入門解答 :::warning :warning: 答案供**參考**,非唯一解。 ::: :::info :information_source: 可直接在網址輸入區打 hackmd.io/@yubo0818/cpp_ans 不需打https:// ::: ## 9/11 作業 1. [a001 : 哈囉](https://zerojudge.tw/ShowProblem?problemid=a001) :::spoiler ans ```cpp= #include <iostream> #include <bits/stdc++.h> using namespace std; int main() { string s; cin>>s; cout<<"hello, "<<s; return 0; } ``` ::: 2. [a002 : 簡易加法](https://zerojudge.tw/ShowProblem?problemid=a002) :::spoiler ans ```cpp= #include <iostream> #include <bits/stdc++.h> using namespace std; int main() { int a,b; cin>>a>>b; cout<<a+b; return 0; } ``` ::: 3. [d049 : 中華民國萬歲!](https://zerojudge.tw/ShowProblem?problemid=d049) :::spoiler ans ```cpp= #include <iostream> #include <bits/stdc++.h> using namespace std; int main() { int a; cin>>a; cout<<(a-1911); return 0; } ``` ::: 4. [d050 : 妳那裡現在幾點了?](https://zerojudge.tw/ShowProblem?problemid=d050) :::spoiler ans ```cpp= #include <iostream> #include <bits/stdc++.h> using namespace std; int main() { int n; cin>>n; if(n<15) cout<<n+24-15; else cout<<n-15; return 0; } ``` ::: 5. [d051 : 糟糕,我發燒了!](https://zerojudge.tw/ShowProblem?problemid=d051) :::spoiler ans ```cpp= #include <iostream> #include <bits/stdc++.h> using namespace std; int main() { double a; cin>>a; a=(a - 32) * 5 / 9; cout<<fixed<<setprecision(3); cout<<a; return 0; } ``` ::: 6. [d073 : 分組報告](https://zerojudge.tw/ShowProblem?problemid=d073) :::spoiler ans ```cpp= #include <iostream> #include <bits/stdc++.h> using namespace std; int main() { int n; cin>>n; if(n%3==0) cout<<n/3; else cout<<(n/3)+1; return 0; } ``` ::: 7. [d827 : 買鉛筆](https://zerojudge.tw/ShowProblem?problemid=d827) :::spoiler ans ```cpp= #include <iostream> #include <bits/stdc++.h> using namespace std; int main() { int n,ans=0; cin>>n; ans=(n/12)*50; ans+=(n%12)*5; cout<<ans; return 0; } ``` ::: ## 10/2 作業 1. [a003 : 兩光法師占卜術](https://zerojudge.tw/ShowProblem?problemid=a003) :::spoiler ans ```cpp= #include <iostream> using namespace std; int main() { int m,d; cin>>m>>d; int s=(m*2+d)%3; if(s==0){ cout<<"普通\n"; } else if(s==1){ cout<<"吉\n"; } else if(s==2){ cout<<"大吉\n"; } return 0; } ``` ::: 2. [d058 : BASIC 的 SGN 函數](https://zerojudge.tw/ShowProblem?problemid=d058) :::spoiler ans if解 ```cpp= #include <iostream> using namespace std; int main() { int n; cin>>n; if(n>0){ cout<<1; } if(n==0){ cout<<0; } if(n<0){ cout<<-1; } return 0; } ``` ::: :::spoiler ans 布林解 ```cpp= #include <iostream> using namespace std; int main() { int n; cin>>n; cout << (n>0) - (n<0); return 0; } ``` ::: 3. [d060 : 還要等多久啊?](https://zerojudge.tw/ShowProblem?problemid=d060) :::spoiler ans ```cpp= #include <iostream> using namespace std; int main() { int n; cin>>n; if(n>25){ cout<<60-n+25; } else(cout<<25-n); return 0; } ``` ::: 4. [d063 : 0 與 1](https://zerojudge.tw/ShowProblem?problemid=d063) :::spoiler ans if解 ```cpp= #include <iostream> using namespace std; int main() { int n; cin>>n; if(n==0){ cout<<1; } if(n==1){ cout<<0; } return 0; } ``` ::: :::spoiler ans 布林解 ```cpp= #include <iostream> using namespace std; int main() { bool n; cin>>n; cout<<(!n); return 0; } ``` ::: 5. [d064 : ㄑㄧˊ 數?](https://zerojudge.tw/ShowProblem?problemid=d064) :::spoiler ans ```cpp= #include <iostream> using namespace std; int main() { int n; cin>>n; if(n%2==0){ cout<<"Even"; } if(n%2==1){ cout<<"Odd"; } return 0; } ``` ::: ## 10/30作業 1. [e835 : p2.表演座位 (Seats)](https://zerojudge.tw/ShowProblem?problemid=e835) :::spoiler ans ```cpp= #include <iostream> using namespace std; int main() { int n; cin>>n; if(n>7500){ cout<<3<<" "; if((n-7500)%25==0){ cout<<(n-7500)/25<<" "<<25; } else cout<<((n-7500)/25)+1<<" "<<n%25; }else if(n>2500){ cout<<2<<" "; if((n-2500)%50==0){ cout<<(n-2500)/50<<" "<<50; } else cout<<((n-2500)/50)+1<<" "<<n%50; }else{ cout<<1<<" "; if(n%25==0){ cout<<n/25<<" "<<25; } else cout<<(n/25)+1<<" "<<n%25; } return 0; } ``` ::: 2. [d669 : 11677 - Alarm Clock](https://zerojudge.tw/ShowProblem?problemid=d669) :::spoiler ans ```cpp= #include <iostream> using namespace std; int main() { int h1,m1,h2,m2; while (cin>>h1>>m1>>h2>>m2) { if(h1==0&&m1==0&&h2==0&&m2==0) break; int one, two; if (h1==0) one=(24*60)+m1; else one=(60*h1)+m1; if (h2==0) two=(24*60)+m2; else two=(60*h2)+m2; if (two<one) two+=(60*24); cout<<two-one<<"\n"; } } ``` ::: 3. [d067 : 格瑞哥里的煩惱 (1 行版)](https://zerojudge.tw/ShowProblem?problemid=d067) :::spoiler ans ```cpp= #include <iostream> using namespace std; int main() { int n; cin>>n; if(n%4!=0||(n%400!=0&&n%100==0)) cout<<"a normal year"; else cout<<"a leap year"; return 0; } ``` ::: 4. [d069 : 格瑞哥里的煩惱 (t 行版)](https://zerojudge.tw/ShowProblem?problemid=d069) :::spoiler ans ```cpp= #include <iostream> using namespace std; int main() { int t; cin>>t; while(t){ int n=0; cin>>n; if(n%4!=0||(n%400!=0&&n%100==0)) cout<<"a normal year"<<"\n"; else cout<<"a leap year"<<"\n"; t--; } return 0; } ``` ::: 5. [d072. 格瑞哥里的煩惱 (Case 版)](https://zerojudge.tw/ShowProblem?problemid=d072) :::spoiler ans ```cpp= #include <iostream> using namespace std; int main() { int t; cin>>t; for(int i=1;i<=t;i++){ int n=0; cin>>n; if(n%4!=0||(n%400!=0&&n%100==0)) cout<<"Case "<<i<<": a normal year"<<"\n"; else cout<<"Case "<<i<<": a leap year"<<"\n"; } return 0; } ``` ::: 6. [d070 : 格瑞哥里的煩惱 (0 尾版)](https://zerojudge.tw/ShowProblem?problemid=d070) :::spoiler ans ```cpp= #include <iostream> using namespace std; int main() { int n=0; while(cin>>n){ if(n==0) break; if(n%4!=0||(n%400!=0&&n%100==0)) cout<<"a normal year"<<"\n"; else cout<<"a leap year"<<"\n"; } return 0; } ``` ::: 7. [d071 : 格瑞哥里的煩惱 (EOF 版)](https://zerojudge.tw/ShowProblem?problemid=d071) :::spoiler ans ```cpp= #include <iostream> using namespace std; int main() { int n=0; while(cin>>n){ if(n%4!=0||(n%400!=0&&n%100==0)) cout<<"a normal year"<<"\n"; else cout<<"a leap year"<<"\n"; } return 0; } ``` ::: ## 11/6 作業 1. [e834 : P1.批發出貨(Wholesale)](https://zerojudge.tw/ShowProblem?problemid=e834) :::spoiler ans ```cpp= #include <iostream> using namespace std; int main() { int m=0; cin>>m; int n=0; while(cin>>n){ if(n==0) break; if(n%m==0) cout<<n/m<<"\n"; else cout<<m-n%m<<"\n"; } return 0; } ``` ::: 2. [a148 : You Cannot Pass?!](https://zerojudge.tw/ShowProblem?problemid=a148) :::spoiler ans ```cpp= #include <iostream> using namespace std; int main() { int n=0; while(cin>>n){ double add=0; int cnt=n; while(n--){ int i; cin>>i; add+=i; } add/=cnt; if(add>59) cout<<"no"<<"\n"; else cout<<"yes"<<"\n"; } return 0; } ``` ::: 3. [d010 : 盈數、虧數和完全數](https://zerojudge.tw/ShowProblem?problemid=d010) :::info :information_source: 已更改。 ::: :::spoiler ans ```cpp= #include<iostream> using namespace std; int main(){ int n; while(cin>>n){ int ans=0; for(int i=1;i<n;i++){ if(n%i==0) ans+=i; } if(ans==n){ cout<<"完全數\n"; } else{ cout<<((ans>n)?"盈數\n":"虧數\n"); } } return 0; } ``` ::: 4. [c067 : 00591 - Box of Bricks](https://zerojudge.tw/ShowProblem?problemid=c067) :::spoiler ans ```cpp= #include <iostream> using namespace std; int main() { int n=0; int m=1; while(cin>>n){ if(n==0) break; int arr[n]={}; int add=0; for(int i=0;i<n;i++){ cin>>arr[i]; add+=arr[i]; } add/=n; int ans=0; for(int i=0;i<n;i++){ if(arr[i]>add) ans+=arr[i]-add; } cout<<"Set #"<<m<<"\n"<<"The minimum number of moves is "<<ans<<"."<<"\n"; m++; } return 0; } ``` ::: 5. [a010 : 因數分解](https://zerojudge.tw/ShowProblem?problemid=a010) :::spoiler ans ```cpp= #include<iostream> using namespace std; int main(){ int a,i,cnt,A; cin>>a; A=a; for(i=2;i<=a;i++){ cnt=0; while(a%i==0){ a=a/i; cnt++; } if(cnt==0) continue; else if(a==1&&cnt>1||a==A&&cnt>1) cout<<i<<"^"<<cnt; else if(a==1||a==A) cout<<i; else if(cnt==1) cout<<i<<" * "; else cout<<i<<"^"<<cnt<<" * "; } } ``` ::: ## 11/13作業 1. [紅豆餅 (Red Bean Cake)](https://drive.google.com/file/d/1fQhs65BQ4hH_LFThNCDNW9TBSi-5rHUy/view?pli=1) :::spoiler ans ```cpp= #include <iostream> using namespace std; #define ff(i,n) for(int i=0;i<n;i++) int main() { int n,b=0,ans=0; cin>>n; int arr[10]={}; ff(i,n){ int a=-2; cin>>a; arr[a]++; if(a>0) b++; } ff(i,6) cout<<arr[i]<<" "; if(arr[0]>b){ ans=b*59+(arr[0]-b)*50; }else if(arr[0]<b){ ans=arr[0]*59+(b-arr[0])*20; }else if(arr[0]==b){ ans=b*59; } cout<<ans; return 0; } ``` ::: 2. [樂透 (Lotto)](https://drive.google.com/file/d/1ySVXhPEzL5Q_inMEesyochYOZBWoZSnu/view) :::spoiler ans ```cpp= #include <iostream> using namespace std; #define ff(i,n) for(int i=0;i<n;i++) int main() { int you[6]={},yes[7]={}; int same=0; bool one=0; ff(i,6){ cin>>you[i]; } ff(i,7){ cin>>yes[i]; } ff(i,6){ if(you[i]==yes[6]){ one=true; } ff(j,6){ if(you[i]==yes[j]){ same++; break; } } } if(one==true&&same==5) cout<<"B"; else if(one==true&&same==4) cout<<"D"; else if(one==true&&same==3) cout<<"F"; else if(one==true&&same==2) cout<<"G"; else if(one==0&&same==6) cout<<"A"; else if(one==0&&same==5) cout<<"C"; else if(one==0&&same==4) cout<<"E"; else if(one==0&&same==3) cout<<"H"; else cout<<"X"; return 0; } ``` ::: 3. [符文 (Runes)](https://drive.google.com/file/d/1sKgqFvKYB3ZwbVDYOydcvVAf1hvyLglu/view) :::spoiler ans ```cpp= #include <iostream> #include <vector> using namespace std; int main() int N, M; if (!(cin >> N >> M)) return 0; vector<int> A(N); for (int i = 0; i < N; ++i) { cin >> A[i]; } vector<int> B(M); for (int i = 0; i < M; ++i) { cin >> B[i]; } int result_pos = -1; for (int i = 0; i <= N - M; ++i) { bool current_match = true; for (int j = 0; j < M; ++j) { if (A[i + j] != B[j]) { current_match = false; break; } } if (current_match) { result_pos = i + 1; break; } } if (result_pos != -1) { cout << result_pos << endl; } else { cout << "not found" << endl; } return 0; } ``` ::: ## 11/27作業 :::spoiler ans 1. ```cpp= #include<iostream> #include<vector> using namespace std; int main(){ int r,c; while(cin>>r>>c){ vector<vector<int>>matrix(105,vector<int>(105)); for(int i=0;i<r;i++){ for(int j=0;j<c;j++){ cin>>matrix[i][j]; } } for(int i=0;i<c;i++){ for(int j=0;j<r;j++){ cout<<matrix[j][i]<<" "; //注意是 [j][i] 不是 [i][j] ! } cout<<"\n"; } } return 0; } ``` ::: :::spoiler ans 2. ```cpp= #include <iostream> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int t, n, m; cin >> t; while (t--){ cin >> n >> m; int a[144]; int len=m*n; for (int i=0; i<len; i++){ cin >> a[i]; } bool go=true; for (int i=0; i<=len/2; i++){ if (a[i]!=a[len-1-i]){ go=false; break; } } if (go){ cout <<"go forward"<<endl; } else { cout <<"keep defending"<<endl; } } } ``` ::: :::spoiler ans 3. ```cpp= #include <bits/stdc++.h> using namespace std; int main () { int i; int n, a1, a2; while (cin >> n) { int a[n+1]={}, b, t = 0; cin >> a1; for (i=1;i<n;i++) { cin >> a2; b = abs(a1-a2); if (a[b] == 0 && b >= 1 && b < n) { a[b] = 1; t++; } a1 = a2; } if (t == n - 1) cout << "Jolly" << endl; else cout << "Not jolly" << endl; } return 0; } ``` ::: :::spoiler ans 4. ```cpp= #include<bits/stdc++.h> using namespace std; typedef long long ll; int main(){ ll n; ll flag =0; ll nn=0; while(cin>>n){ ll a[100000]={0}; flag=0; nn++; vector<ll>v; for(ll i=0;i<n;++i){ ll t; cin>>t; v.push_back(t); } for(ll i=0;i<n;++i){ for(ll j=i;j<n;++j){ ll sum=v[i]+v[j]; if(a[sum]!=0){ flag =1; break; }else{ a[sum]=1; } } } if(flag==1){ cout<<"Case #"<<nn<<": It is not a B2-Sequence."<<endl; } else{ cout<<"Case #"<<nn<<": It is a B2-Sequence."<<endl; } } } ``` ::: ## 12/11作業 :::spoiler ans 1. ```cpp= #include<bits/stdc++.h> using namespace std; int main() { long long int a,b; cin>>a>>b; int n=0; int store; int total=0; int ifcout=0; for(int num=a;num<b+1;num++) { store=num; while(num>0) { num/=10; n+=1; } num=store; while(num>0) { total+=pow(num%10,n); num/=10; } num=store; if(total==store) { cout<<total<<' '; ifcout=1; } total=0; n=0; } if(ifcout==0) { cout<<"none"; } } ``` ::: :::spoiler ans 2. ```cpp= #include <iostream> #include <vector> using namespace std; int main() { int n, m; while(cin>>n>>m) { vector<int> foods(n); //foods = {0,0,0,...,0} for(int i = 1; i <= n; i++) { cin>>foods[i]; foods[i] += foods[i-1]; } for(int i = 0; i < m; i++) { int start, end; cin>>start>>end; cout<<foods[end] - foods[start - 1]<<endl; } } return 0; } ``` ::: :::spoiler ans 3. ```cpp= #include <bits/stdc++.h> using namespace std; int main() { int a,b,c,r;double x1,x2,x; cin>>a>>b>>c; if (a!=0){ r=b*b - 4*a*c; if(r>0){ x1=(b*-1 + sqrt(r))/(2*a); x2=(b*-1 - sqrt(r))/(2*a); if(x1>x2) cout<<"Two different roots x1="<<x1<<" , x2="<<x2; else cout<<"Two different roots x1="<<x2<<" , x2="<<x1; } else if(r==0){ x=(b*-1 + sqrt(r))/(2*a); cout<<"Two same roots x="<<x; } else cout<<"No real root"; } else cout<<"No real root"; return 0; } ``` ::: :::spoiler ans 4. ```cpp= #include <bits/stdc++.h> using namespace std; int gcd(int m, int n) { int r = 0; while(n != 0) { r = m % n; m = n; n = r; } return m; } int main() { long long int l,n,h[10000]; while (cin>>n){ if(n==0) return 0; int g=0; for(int i=1;i<n;i++){ for(int j=i+1;j<=n;j++){ g+=gcd(i,j); } } cout<<g<<endl; } return 0; } ``` ::: :::spoiler ans 5. ```cpp= #include <iostream> #include <sstream> using namespace std; int main() { int t; string s; char head, tail, pre; if (!(cin >> t)) return 0; getline(cin, s); while (t--) { bool loop = true; int len = 1; if (!getline(cin, s) || s.empty()) continue; head = s[0]; tail = s[s.size() - 1]; if (head == tail) { cout << "NO LOOP\n"; continue; } stringstream ss(s); string x; ss >> x; pre = x[1]; while (ss >> x) { len++; if (pre == x[0]) { loop = false; break; } pre = x[1]; } if (len > 1 && loop) { cout << "LOOP\n"; } else { cout << "NO LOOP\n"; } } return 0; } ``` :::