# APCS歷屆 APCS歷屆答案紀錄 ## 目前規劃 1.寫完第1題和第2題(未完成) 2.把第1題和第2題的解答、考點、程式碼修好一點(未完成) 3.增加第1題和第2題的詳解(未完成) 4.寫完第3題和第4題(未完成) 5.把第3題和第4題的解答、考點、程式碼修好一點(未完成) 6.增加第3題和第4題的詳解(未完成) 7.增加C語言的參考解答和JAVA的參考解答(未完成) ## 2016年 3月 ### 第 1 題 成績指標 題目:https://zerojudge.tw/ShowProblem?problemid=b964 考點:迴圈,if-else,sort語法 python參考答案 ```python= a=int(input()) high,low=-1,101 nums=[int(i) for i in input().split()] nums.sort() for i in range(a): #輸出第一行 if i:print(end=" ") print(nums[i],end="") #處理best和worst if nums[i]<60: if nums[i]>high:high=nums[i] else: if nums[i]<low:low=nums[i] print() #處理最低及格分數 if high!=-1: print(high) else: print("best case") #處理最高不及格分數 if low!=101: print(low) else: print("worst case") ``` c++參考答案 ```cpp! #include<bits/stdc++.h> using namespace std; int main(){ int n; cin>>n; int a[25]; for(int i=0;i<n;i++){ cin>>a[i]; } sort(a,a+n); int high=101,low=-1; for(int i=0;i<n;i++){ if(i)cout<<" "; cout<<a[i]; if(a[i]<60){ if(a[i]>low)low=a[i]; }else{ if(a[i]<high)high=a[i]; } } cout<<endl; if(low!=-1){ cout<<low<<endl; }else{ cout<<"best case"<<endl; } if(high!=101){ cout<<high<<endl; }else{ cout<<"worst case"<<endl; } return 0; } ``` ### 第2題 矩陣轉換 題目:https://zerojudge.tw/ShowProblem?problemid=b965 考點:迴圈,二維矩陣 注意:題目寫給的輸入是矩陣B,而不是矩陣A,注意看題目 python參考答案 ```python! while True: #處理輸入 try: R,C,M = [int(i) for i in input().split()] except: break matrix = [] for i in range(R): matrix.append([int(i) for i in input().split()]) order = [int(i) for i in input().split()] order=order[::-1] #以下是把矩陣B復原為矩陣A的過程 for k in order: if k==0: tmp=[] for i in range(C-1,-1,-1): tmp2=[] for j in matrix: tmp2.append(j[i]) tmp.append(tmp2) matrix=tmp tmp=R R=C C=tmp else: matrix=matrix[::-1] #輸出復原好的矩陣A print(R,C) for i in matrix: for j in range(len(i)): if j:print(end=" ") print(i[j],end="") print() ``` c++參考答案 ```cpp! #include<bits/stdc++.h> using namespace std; int main(){ int R,C,M; while(cin>>R>>C>>M){ int matrix[15][15],matrix2[15][15]; int nowuse=1; for(int i=0;i<R;i++){ for(int j=0;j<C;j++){ cin>>matrix[i][j]; } } int order[10]; for(int i=M-1;i>=0;i--){ cin>>order[i]; } for(int k=0;k<M;k++){ if(order[k]==0){ if(nowuse==1){ for(int i=0;i<C;i++){ for(int j=0;j<R;j++){ matrix2[i][j]=matrix[j][C-i-1]; } } nowuse=2; }else{ for(int i=0;i<C;i++){ for(int j=0;j<R;j++){ matrix[i][j]=matrix2[j][C-i-1]; } } nowuse=1; } swap(R,C); }else{ if(nowuse==1){ for(int i=0;i<R/2;i++){ for(int j=0;j<C;j++){ swap(matrix[i][j],matrix[R-i-1][j]); } } }else{ for(int i=0;i<R/2;i++){ for(int j=0;j<C;j++){ swap(matrix2[i][j],matrix2[R-i-1][j]); } } } } } cout<<R<<" "<<C<<endl; if(nowuse==1){ for(int i=0;i<R;i++){ for(int j=0;j<C;j++){ if(j)cout<<" "; cout<<matrix[i][j]; } cout<<endl; } }else{ for(int i=0;i<R;i++){ for(int j=0;j<C;j++){ if(j)cout<<" "; cout<<matrix2[i][j]; } cout<<endl; } } } return 0; } ``` ## 2016年 10月 ### 第1題 三角形辨別 題目:https://zerojudge.tw/ShowProblem?problemid=c294 考點:if-else,sort語法 python參考答案 ```python! a,b,c=sorted([int(i) for i in input().split()]) print(a,b,c) if a+b<=c: print("No") elif a*a+b*b<c*c: print("Obtuse") elif a*a+b*b==c*c: print("Right") else: print("Acute") ``` c++參考答案 ```cpp! #include<bits/stdc++.h> using namespace std; int main(){ int k[3]; for(int i=0;i<3;i++){ cin>>k[i]; } sort(k,k+3); for(int i=0;i<3;i++){ if(i)cout<<" "; cout<<k[i]; } cout<<endl; int a=k[0],b=k[1],c=k[2]; if(a+b<=c){ cout<<"No"; }else if(a*a+b*b<c*c){ cout<<"Obtuse"; }else if(a*a+b*b==c*c){ cout<<"Right"; }else{ cout<<"Acute"; } return 0; } ``` ### 第2題 最大和 題目:https://zerojudge.tw/ShowProblem?problemid=c295 考點:max語法,if-else python參考答案 ```python! n,m=[int(i) for i in input().split()] a=[] S=0 for i in range(n): tmp=max([int(i) for i in input().split()]) S+=tmp a.append(tmp) print(S) test=True#用來測試有沒有數字可以整除S for i in a: if S%i==0: if not test: print(end=" ") test=False print(i,end="") if test: print(-1) ``` c++參考答案 ```cpp! #include<bits/stdc++.h> using namespace std; int main(){ int n,m; cin>>n>>m; int a[20]; int S=0; for(int i=0;i<n;i++){ int tmp=0; for(int j=0;j<m;j++){ int A; cin>>A; tmp=max(tmp,A); } a[i]=tmp; S+=tmp; } cout<<S<<endl; bool test=true; for(int i=0;i<n;i++){ if(S%a[i]==0){ if(!test)cout<<" "; cout<<a[i]; test=false; } } if(test)cout<<-1; return 0; } ``` ## 2017年 3月 ### 第1題 秘密差 題目:https://zerojudge.tw/ShowProblem?problemid=c290 考點:變數型態轉換,迴圈 python參考解答 ```python! n=input() a=0 b=0 for i in range(0,len(n),2): a+=int(n[i]) for i in range(1,len(n),2): b+=int(n[i]) print(abs(a-b)) ``` c++參考解答 ```cpp! #include<bits/stdc++.h> using namespace std; int main(){ string n; cin>>n; int a=0,b=0; for(int i=0;i<n.size();i+=2){ a+=n[i]-'0'; } for(int i=1;i<n.size();i+=2){ b+=n[i]-'0'; } cout<<abs(a-b); return 0; } ``` ### 第2題 小群體 題目:https://zerojudge.tw/ShowProblem?problemid=c291 考點:並查集 python參考解答 ```python! a=int(input()) order=[int(i) for i in input().split()] dsu=[] for i in range(0,a): dsu.append(i) def find(b): if dsu[b]==b:return b dsu[b]=find(dsu[b]) return dsu[b] def uni(b,c): dsu[find(b)]=dsu[find(c)] answer=a for i in range(0,a): if find(i)!=find(order[i]): uni(i,order[i]) answer-=1 print(answer) ``` c++參考解答 ```cpp! #include<bits/stdc++.h> using namespace std; int dsu[50010]; int Find(int a){ if(dsu[a]==a)return a; dsu[a]=Find(dsu[a]); return dsu[a]; } void unio(int a,int b){ dsu[Find(a)]=dsu[Find(b)]; return; } int main(){ int n; cin>>n; for(int i=0;i<n;i++){ dsu[i]=i; } int answer=n; for(int i=0;i<n;i++){ int tmp; cin>>tmp; if(Find(i)!=Find(tmp)){ answer--; unio(i,tmp); } } cout<<answer; return 0; } ``` ## 2017年 10月 ### 第1題 邏輯運算子 題目:https://zerojudge.tw/ShowProblem?problemid=c461 考點:if-else python參考解答 ```python! while True: #tmp=input() #print(tmp) a,b,c=[int(i) for i in input().split()] test=True if c==1: if a!=0 and b!=0: print("AND") test=False else: if a==0 or b==0: print("AND") test=False if c==1: if a!=0 or b!=0: print("OR") test=False else: if a==0 and b==0: print("OR") test=False if c==1: if ( a!=0 and b==0 ) or ( a==0 and b!=0 ): print("XOR") test=False else: if ( a==0 and b==0 ) or ( a!=0 and b!=0 ): print("XOR") test=False if test: print("IMPOSSIBLE") try: tmp=input() print() except: break ``` C++參考解答 ```cpp! #include<bits/stdc++.h> using namespace std; int main(){ int a,b,c; cin>>a>>b>>c; bool test=true; if(c==1){ if(a!=0 && b!=0){ cout<<"AND"<<endl; test=false; } }else{ if(a==0 || b==0){ cout<<"AND"<<endl; test=false; } } if(c==1){ if(a!=0 || b!=0){ cout<<"OR"<<endl; test=false; } }else{ if(a==0 && b==0){ cout<<"OR"<<endl; test=false; } } if(c==1){ if((a!=0 && b==0) || (a==0 && b!=0)){ cout<<"XOR"<<endl; test=false; } }else{ if((a==0 && b==0) || (a!=0 && b!=0)){ cout<<"XOR"<<endl; test=false; } } if(test)cout<<"IMPOSSIBLE"; return 0; } ``` ### 第2題 交錯字串 題目:https://zerojudge.tw/ShowProblem?problemid=c462 考點:迴圈,if-else python參考解答 ```python! k=int(input()) s=input() upper=s[0].isupper() con=1 if con%k==0:tmpanswer=con else: tmpanswer=0 answer=tmpanswer for i in s[1:]: if upper: if i.isupper(): if con==k: tmpanswer=k else: con+=1 if con==k: tmpanswer+=con answer=max(answer,tmpanswer) else: upper=False if con!=k:tmpanswer=0 con=1 if con==k: tmpanswer+=con answer=max(answer,tmpanswer) else: if i.islower(): if con==k: tmpanswer=k else: con+=1 if con==k: tmpanswer+=con answer=max(answer,tmpanswer) else: upper=True if con!=k:tmpanswer=0 con=1 if con==k: tmpanswer+=con answer=max(answer,tmpanswer) print(answer) ``` ```cpp! #include<bits/stdc++.h> using namespace std; string s; int main(){ int k; cin>>k; cin.get(); getline(cin,s); if(s=="")getline(cin,s); bool upper=isupper(s[0]); int con=1; int tmpanswer=0; if(con%k==0)tmpanswer+=con; else tmpanswer=0; int answer=tmpanswer; for(int i=1;i<s.size();i++){ if(upper){ if(isupper(s[i])){ if(con==k){ tmpanswer=k; }else{ con++; if(con==k){ tmpanswer+=k; answer=max(answer,tmpanswer); } } }else{ if(con!=k)tmpanswer=0; con=1; if(con==k){ tmpanswer+=con; answer=max(answer,tmpanswer); } upper=false; } }else{ if(islower(s[i])){ if(con==k){ tmpanswer=k; }else{ con++; if(con==k){ tmpanswer+=k; answer=max(answer,tmpanswer); } } }else{ if(con!=k)tmpanswer=0; con=1; if(con==k){ tmpanswer+=con; answer=max(answer,tmpanswer); } upper=true; } } } cout<<answer; //3 9 17 return 0; } ``` ## APCS類似題 ### 小崴的特殊編碼 考點:if-else,IO優化 IO優化參考資料: https://yuihuang.com/python-io-optimize/ https://blog.csdn.net/CAU_Ayao/article/details/81985103 https://blog.csdn.net/mishi_zcf/article/details/52486974 題目:https://zerojudge.tw/ShowProblem?problemid=e283 python參考解答 ```python! import sys for i in sys.stdin: n=int(i) for j in range(n): tmp=sys.stdin.readline().strip() if tmp=="0 1 0 1": print(end="A") elif tmp=="0 1 1 1": print(end="B") elif tmp=="0 0 1 0": print(end="C") elif tmp=="1 1 0 1": print(end="D") elif tmp=="1 0 0 0": print(end="E") elif tmp=="1 1 0 0": print(end="F") print() ``` C++參考解答 ```cpp! #include<bits/stdc++.h> using namespace std; int main(){ cin.tie(0); ios::sync_with_stdio(false); int n; while(cin>>n){ cin.get(); for(int i=0;i<n;i++){ string tmp; getline(cin,tmp); if(tmp=="0 1 0 1"){ cout<<"A"; }else if(tmp=="0 1 1 1"){ cout<<"B"; }else if(tmp=="0 0 1 0"){ cout<<"C"; }else if(tmp=="1 1 0 1"){ cout<<"D"; }else if(tmp=="1 0 0 0"){ cout<<"E"; }else if(tmp=="1 1 0 0"){ cout<<"F"; } } cout<<'\n'; } return 0; } ```