Yunn0527
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Versions and GitHub Sync Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
Invitee
Publish Note

Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

Your note will be visible on your profile and discoverable by anyone.
Your note is now live.
This note is visible on your profile and discoverable online.
Everyone on the web can find and read all notes of this public team.
See published notes
Unpublish note
Please check the box to agree to the Community Guidelines.
View profile
Engagement control
Commenting
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
  • Everyone
Suggest edit
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
Emoji Reply
Enable
Import from Dropbox Google Drive Gist Clipboard
   owned this note    owned this note      
Published Linked with GitHub
1
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
# 考古題 - 以後會持續更新參考程式碼(由於有人嫌我的code太暴力所以只好全部重寫:cry:) 盡量要練到對一題半以上,這樣才有機會三級分(1題全對+1題測資正確50%) --- ## 注意事項 * 如果程式執行時間超時(TLE),就算測資正確也可能拿不到分數 * 注意輸出格式要求,多或少空格都會沒有分數 * 仔細檢查陣列大小,如果超過題目限制大小或太小,都有可能造成錯誤 * 記得做 i/o 優化 程式碼如下(背一下,有時能卡時間),以下優化前後差別 * 詳解先不要著急點開,練習的時候會思考很多天都是很正常的 ![image](https://hackmd.io/_uploads/Hyx84pvZ0.png) ![image](https://hackmd.io/_uploads/H16VNTvbC.png) :::spoiler i/o優化 ```cpp //記得要寫在main()裡 ios_base::sync_with_stdio(false); cin.tie(nullptr); ``` ::: * 解不出來能暴力就暴力,只要子題組正確率越高就越有機會增加級分 * 三四題如果還有時間的話可以嘗試暴力或印測資(以確保拿到前兩題為前提) ## 二級分考古題 :warning: :較難 - [ ] 1. [數字遊戲](https://zerojudge.tw/ShowProblem?problemid=i399) :::spoiler c++ ```cpp #include <bits/stdc++.h> using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); set<int>s; int a,b,c; cin >> a >> b>> c; s.insert(a); s.insert(b); s.insert(c); cout << 4-s.size() << " "; vector<int> v; for(auto e:s){ v.push_back(e); } for(int i= v.size()-1;i>=0;i--){ cout << v[i] <<" " ; } cout << endl; return 0; } ``` ::: :::spoiler Python(暴力) ```python ar = list(map(int, input().split())) a = 0 b = 0 for i in range(3): if ar[0] == ar[i]: a += 1 for i in range(1, 3): if ar[1] == ar[i]: b += 1 if a > b: if a == 3: print(f"{a} {ar[0]}") elif a == 2: del ar[0:1] ar.sort(reverse=True) x = ar[0] y = ar[1] print(f"{a} {x} {y}") elif b > a: del ar[1:2] ar.sort(reverse=True) x = ar[0] y = ar[1] print(f"{b} {x} {y}") else: ar.sort(reverse=True) x = ar[0] y = ar[1] z = ar[2] print(f"1 {x} {y} {z}") ``` ::: - [ ] 2. [程式交易](https://zerojudge.tw/ShowProblem?problemid=h081) :::spoiler Python ```python a,b=map(int,input().split()) c=[int(x) for x in input().split()] d=0 e=c[0] f=0 for i in range(1,a): if f==0: if e<=c[i]-b: d=d+c[i]-e e=c[i] f=1 else: if e>=c[i]+b: e=c[i] f=0 print(d) ``` ::: - [ ] 3. [修補圍籬](https://zerojudge.tw/ShowProblem?problemid=g595) :::spoiler Python ```python n=int(input()) a=[int(x) for x in input().split()] ans=0 for i in range(len(a)): if i==0: if a[i]==0: ans+=a[i+1] elif i==(len(a)-1): if a[i]==0: ans+=a[i-1] else: if a[i]==0: if a[i+1]>=a[i-1]: ans+=a[i-1] else: ans+=a[i+1] print(ans) ``` ::: - [ ] 4. [七言對聯](https://zerojudge.tw/ShowProblem?problemid=g275):warning: :::spoiler c++ ```cpp #include <bits/stdc++.h> using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n ; while(n--){ string s; int a[7],b[7]; for(int i = 0; i < 7;i++){ cin >> a[i]; } for(int i = 0; i < 7;i++){ cin >> b[i]; } if(a[1] == a[3] or b[1] == b[3] or a[1]!=a[5] or b[1]!=b[5] ){ s+='A'; } if(a[6] !=1 or b[6] !=0){ s+='B'; } if(a[1]==b[1] or a[3] ==b[3] or a[5] == b[5] ){ s+='C'; } if(s.size()==0){ cout <<"None" << endl; } else{ cout << s << endl; } } return 0; } ``` ::: :::spoiler Python ```python n=int(input()) for i in range(1,n+1): ans=[] a=[int(x) for x in input().split()] b=[int(x) for x in input().split()] if not (a[1]!=a[3] and a[1]==a[5] and b[1]!=b[3] and b[1]==b[5]): ans.append("A") if not (a[6]==1 and b[6]==0): ans.append("B") if not (a[1]!=b[1] and a[3]!=b[3] and a[5]!=b[5]): ans.append("C") if not bool(ans): print("None") else: print(*ans,sep="") ``` ::: - [ ] 5. [購買力](https://zerojudge.tw/ShowProblem?problemid=f605) :::spoiler Python ```python n,d=map(int,input().split()) su=0 ans=0 for i in range(n): s=list(map(int,input().split())) if max(s)-min(s)>=d: su+=((s[0]+s[1]+s[2])//3) ans+=1 print(ans,su) ``` ::: - [ ] 6. [人力分配](https://zerojudge.tw/ShowProblem?problemid=f312) :::spoiler Python ```python x1=[int(x) for x in input().split()] x2=[int(x) for x in input().split()] n=int(input()) mx=-10000 for i in range(n+1): mx=max(mx,x1[0]*i*i+x1[1]*i+x1[2]+x2[0]*(n-i)*(n-i)+x2[1]*(n-i)+x2[2]) print(mx) ``` ::: - [ ] 7. [購物車](https://zerojudge.tw/ShowProblem?problemid=f579) :::spoiler Python ```python a,b=map(int,input().split()) c=int(input()) z=[] ans=0 for j in range(c): z=list(map(int,input().split())) aa=z.count(a) bb=z.count(-a) aa=aa-bb cc=z.count(b) d=z.count(-b) bb=cc-d if aa>=1 and bb>=1: X+=1 print(ans) ``` ::: - [ ] 8. [猜拳](https://zerojudge.tw/ShowProblem?problemid=h026) :warning: :::spoiler Python ```python f=int(input()) n=int(input()) a=[int(x) for x in input().split()] ans=[] type=[0,2,5] time=0 pre=0 k=0 last_f=-1 for x in a: k+=1 if f-x==-2 or f-x==-3 or f-x==5: ans.append(f) print(" ".join(map(str,ans)),': Won at round',k) break elif f-x==2 or f-x==3 or f-x==-5: ans.append(f) print(" ".join(map(str,ans)),': Lost at round',k) break if x==f: time+=1 ans.append(f) if time<2: pre=x last_f=f f=x elif time==2: last_f=f if pre==5: f=2 elif pre==2: f=0 elif pre==0: f=5 time=0 if k==n and last_f==a[n-1]: print(" ".join(map(str,ans)),': Drew at round',k) ``` ::: - [ ] 9. [籃球比賽](https://zerojudge.tw/ShowProblem?problemid=e286) :::spoiler Python ```python a1 = [int(x)for x in input().split()] a2 = [int(x)for x in input().split()] b1 = [int(x)for x in input().split()] b2 = [int(x)for x in input().split()] print(str(sum(a1))+":"+str(sum(a2))) print(str(sum(b1))+":"+str(sum(b2))) if sum(a1)> sum(a2) and sum(b1)>sum(b2): print("Win") elif sum(a1)< sum(a2) and sum(b1)<sum(b2): print("Lose") else: print("Tie") ``` ::: - [ ] 10. [邏輯運算子](https://zerojudge.tw/ShowProblem?problemid=c461) :warning: :::spoiler c++ ```cpp #include <bits/stdc++.h> using namespace std; int main(){ int a,b; int i; cin >> a >> b >> i; if(a == 0 and b == 0 ){ if(i== 0){ cout << "AND" <<endl<<"OR" <<endl<<"XOR" ; }else{ cout << "IMPOSSIBLE" << endl; } } else if((a== 0 and b != 0) or((a!= 0 and b == 0))){ if(i == 0){ cout <<"AND" << endl; }else { cout << "OR" << endl <<"XOR" << endl; } } if(a != 0 and b != 0){ if(i == 0){ cout << "XOR" << endl; } else{ cout << "AND" << endl <<"OR" << endl; } } return 0; } ``` ::: ::: spoiler Python ```python a,b,res = map(int,input().split()) if a == 0 and b == 0: if res == 0: print("AND") print("OR") print("XOR") else: print("IMPOSSIBLE") elif (a== 0 and b != 0) or (a!= 0 and b == 0): if res == 0: print("AND") else: print("OR") print("XOR") if a!= 0 and b != 0: if res == 0: print("XOR") else: print("AND") print("OR") ``` ::: - [ ] 11. [秘密差](https://zerojudge.tw/ShowProblem?problemid=c290) :::spoiler Python ```python a=str(input()) b=0 c=0 for i in range(0,len(a)): if i%2==0: b+=a[i] else: c+=a[i] print(abs(b-c)) ``` ::: - [ ] 12. [三角形辨別](https://zerojudge.tw/ShowProblem?problemid=c294) :::spoiler Python ```python a=[int(x)for x in input().split()] a.sort() print(a) if a[0]+a[1]<=a[2]: print("No") elif a[0]*a[0]+a[1]*a[1]==a[2]*a[2]: print("Right") elif a[0]*a[0]+a[1]*a[1]>a[2]*a[2]: print("Acute") else: print("Obtuse") ``` ::: - [ ] 13. [成績指標](https://zerojudge.tw/ShowProblem?problemid=b964) :::spoiler Python ```python I = int(input()) n = [int(I) for I in input().split()] n.sort() print(" ".join(map(str, n))) a = 0 b = 0 n.sort(reverse=True) for j in range(I): if n[j] < 60: print(n[j]) b += 1 break if b == 0: print("best case") n.sort() for i in range(I): if n[i] >= 60: print(n[i]) a += 1 break if a == 0: print("worst case") ``` ::: - [ ] 14. [巴士站牌](https://zerojudge.tw/ShowProblem?problemid=i428) :::spoiler Python ```python= n=int(input()) a=[list(map(int,input().split()))for _ in range(n)] x,y=a[0] mx=0 mn=10000 for xx,yy in a[1:]: ds=abs(yy-y)+abs(xx-x) mn=min(ds,mn) mx=max(ds,mx) x=xx y=yy print(x,y) ``` ::: - [ ] 15. [程式考試](https://zerojudge.tw/ShowProblem?problemid=j605) :::spoiler Python ```python while True: try: k = int(input()) ti= [] sc= [] for i in range(k): t,s = map(int, input().split()) ti.append(t) sc.append(s) ans=max(score)-k-sc.count(-1)*2 print(0 if ans < 0 else ans, ti[score.index(max(sc))]) except: break ``` ::: - [ ] 16. [路徑偵測](https://zerojudge.tw/ShowProblem?problemid=k731) :warning: :::spoiler c++ ```cpp #include<bits/stdc++.h> using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; int px= 0,py= 0; //1 E 2W 3 S 4 N int ew = 1; int l = 0 ,r= 0,re = 0; while(n--){ int x,y; cin >> x >> y; if(ew == 1){ if(x<px){ re++; px = x; ew = 2; } else if(y<py){ r++; py = y; ew = 3; } else if(y>py){ l++; py = y; ew = 4; } else if(x>px){ px = x; } } if(ew ==2 ){ if(x-px >0){ re++; px = x; ew = 1; } else if(y-py >0){ r++; py = y; ew = 4; } else if(y-py < 0){ l++; py = y; ew = 3; } else if(x<px){ px = x; } } if(ew ==3 ){ if(y-py >0){ re++; py = y; ew = 4; } else if(x-px >0){ l++; px = x; ew = 1; } else if(x-px < 0){ r++; px = x; ew = 2; } else if(y<py){ py = y; } } if(ew ==4 ){ if(y-py <0){ re++; py = y; ew = 3; } else if(x-px >0){ r++; px = x; ew = 1; } else if(x-px < 0){ l++; px = x; ew = 2; } else if(y>py){ py = y; } } } cout << l <<" " << r << " " << re << endl; return 0; } ``` ::: :::spoiler Python直觀解 ```python n=int(input()) L=[[int(x) for x in input().split()]for _ in range(n)] dl=[0,0,0] a,b,c=0,0,0 for i in L: if i[0]==dl[0]: if i[1]>dl[1]: if dl[2]==0: a+=1 elif dl[2]==1: c+=1 elif dl[2]==2: b+=1 else: None dl[2]=3 else: if dl[2]==0: b+=1 elif dl[2]==1: None elif dl[2]==2: a+=1 else: c+=1 dl[2]=1 else: if i[0]>dl[0]: if dl[2]==0: None elif dl[2]==1: a+=1 elif dl[2]==2: c+=1 else: b+=1 dl[2]=0 else: #x- if dl[2]==0: c+=1 elif dl[2]==1: b+=1 elif dl[2]==2: None else: a+=1 dl[2]=2 dl[0],dl[1]=i[0],i[1] print(a,b,c) ``` ::: ::: spoiler Python效率解 ```python n=int(input()) px,py=map(int,input().split()) prev_d=0 left,right,back=0,0,0 for i in range(n-1): x,y=map(int,input().split()) if x > px: d = 0 elif y<py:d =1 elif x<px:d =2 else: d = 3 turn = (d-prev_d)%4 if turn==3: left += 1 elif turn==1: right += 1 elif turn==2: back += 1 px,py = x,y prev_d = d print(left,right,back) ::: - [ ] 17. [機械鼠](https://zerojudge.tw/ShowProblem?problemid=m370) :::spoiler c++ ```cpp #include <bits/stdc++.h> using namespace std; int main(){ int x,n; cin >> x >>n; int a[n]; vector<int> l,s; for(int i = 0; i <n;i++){ cin >> a[i]; if(a[i]>x){ l.push_back(a[i]); } else{ s.push_back(a[i]); } } if(s.size()>l.size()){ cout << s.size() <<" "; sort(s.begin(),s.end()); cout << s[0] << endl; } else{ cout << l.size() <<" "; sort(l.begin(),l.end()); cout << l[l.size()-1] << endl; } return 0; } ``` ::: :::spoiler Python ```python n,m = map(int,input().split()) x = [int(x)for x in input().split()] c = sorted(x) a = 0 b= 0 for i in c: if i < n: a+=1 else: b+=1 if b> a: print(b,c[m-1]) else: print(a,c[0]) ``` ::: ::: spoiler Python(微優化) ```python x,n = map(int,input().split()) a = [int(i) for i in input().split()] low =0 hi = 0 alo = min(a) ahi = max(a) for i in a: if i < x: low+=1 else: hi+=1 if low > hi: print(low,alo) else: print(hi,ahi) ``` ::: - [ ] 18. [遊戲選角](https://zerojudge.tw/ShowProblem?problemid=m931) :::spoiler Python ```python n=int(input()) a=[[int(x) for x in input().split()]for _ in range(n)] an=[0]*n for i in range(n): an[i]=a[i][0]**2+a[i][1]**2 an.sort() an.reverse() for i in range(n): if an[1]==a[i][0]**2+a[i][1]**2: print(a[i][0],a[i][1]) ``` ::: - [ ] 19. [特技表演](https://zerojudge.tw/ShowProblem?problemid=o076) :::spoiler c++ ```cpp #include <bits/stdc++.h> using namespace std; int a[105]; int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; for(int i= 0; i < n;i++){ cin >> a[i]; } int ans = 0; for(int i= 0; i < n;i++){ int mx = a[i]; int s = 0; for(int j = i+1;j < n;j++){ if(mx>a[j]){ mx = a[j]; s++; } else{ break; } } if(s>ans){ ans = s; } } cout << ans+1 << endl; return 0; } ``` ::: :::spoiler Python ```python n = int(input()) a=[int(x) for x in input().split()] ans = 0 for i in range(0,n): mx = a[i] s = 0 for j in range(i+1,n): if mx > a[j]: mx = a[j] s+=1 else: break if s > ans: ans = s print(ans +1) ``` ::: - [ ] 20.[裝飲料](https://zerojudge.tw/ShowProblem?problemid=o711) :::spoiler Python ```python a=int(input()) w1,w2,h1,h2 = map(int,input().split()) w1 = w1*w1 w2 = w2*w2 fu = map(int,input().split()) wh1 = w1*h1 wh2 = w2*h2 max = 0 hv = 0 for i in fu: if hv >=wh1+wh2: break if hv< wh1: if i + hv <=wh1: gg = i/w1 hv+=i if gg >max: max = gg elif i + hv>wh1 and i+hv<wh1+wh2: gg = (wh1-hv)/w1+(i-(wh1-hv))/w2 hv+=i if gg > max: max = gg else: gg = (wh1-hv)/w1+h2 hv = wh1+wh2 if gg > max: max = gg elif hv == wh1: if i<=wh2: gg = i/w2 hv+=i if gg > max: max = gg else: hv+=wh2 gg = h2 if gg > max: max = gg elif hv>wh1 and hv < wh2: if wh2-(hv-wh1)>i: gg =i/w2 hv+=i if gg> max: max = gg else: gg = (wh2-(hv-wh1))/w2 hv+=wh2-(hv-wh1) if gg > max : max = gg print(int(max)) ``` ::: ## 三級分考古題 二維陣列很重要:no_mouth: - [ ] 1. [字串解碼](https://zerojudge.tw/ShowProblem?problemid=i400) :::spoiler c++ ```cpp #include <bits/stdc++.h> using namespace std; string ch(string ss){ if(ss.size()%2 == 1){ string cg; for(int i = (ss.size()-1)/2+1;i< ss.size();i++){ cg+=ss[i]; } cg+=ss[(ss.size()-1)/2]; for(int i = 0; i < (ss.size()-1)/2;i++){ cg+=ss[i]; } return cg; } else{ string cg; for(int i = ss.size()/2; i < ss.size();i++){ cg+=ss[i]; } for(int i = 0; i < ss.size()/2;i++){ cg+=ss[i]; } return cg; } } int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int m,n; string a; cin >> m >> n; char c[m][n]; for(int i = 0; i < m;i++){ for(int j = 0 ; j < n;j++){ cin >> c[i][j]; } } cin >> a; for(int i = m-1; i >= 0;i--){ deque<char>q; int ct = 0; for(int j = n-1 ; j >=0;j--){ if(c[i][j]=='0'){ q.push_front(a[j]); } else{ ct++; q.push_back(a[j]); } } a.clear(); for(int j = 0 ; j < n;j++){ a+=q.front(); q.pop_front(); } if(ct%2==1 ){ a = ch(a); } } cout << a << endl; return 0;} ``` ::: ::: spoiler c++(substr) ```c++ #include <bits/stdc++.h> using namespace std; int m,n; string t; int main(){ cin >> m >> n; vector<string> v; for(int i = 0; i < m ;i++){ string ssr; cin >> ssr; v.push_back(ssr); } cin >> t; for(int i =m-1;i >=0;i--){ int sum = 0; string s; deque<char>tt; for(int j = n-1;j >=0;j--){ if(v[i][j] == '1'){ sum++; tt.push_back(t[j]); } else{ tt.push_front(t[j]); } } t.clear(); for(int j = 0 ; j < n;j++){ t+=tt.front(); tt.pop_front(); } string stt =t; if(sum %2 != 0){ if(n%2==0){ stt = t.substr(t.size()/2)+t.substr(0,t.size()/2); } else{ stt = t.substr(t.size()/2+1)+t[t.size()/2]+t.substr(0,t.size()/2); } } t = stt; } cout << t << endl; return 0;} ``` ::: - [ ] 2. [贏家預測](https://zerojudge.tw/ShowProblem?problemid=h082) :::spoiler c++ ```cpp #include <bits/stdc++.h> #define ll long long int using namespace std; int n,m; ll s[100000]; ll t[100000]; int ot[100000] = {0}; int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); vector<int> v; cin >> n >> m; for(int i =1; i <= n;i++ ){ cin >> s[i]; } for(int i = 1; i <=n;i++){ cin >> t[i]; } for(int i = 0; i < n;i++){ int a; cin >>a; v.push_back(a); } vector<int> lnx; vector<int> wnx; while(v.size()>1){ for(int i = 0; i <= v.size()-2;i+=2){ ll a = s[v[i]]; ll b = t[v[i]]; ll c = s[v[i+1]]; ll d = t[v[i+1]]; if(a*b >=c*d){ s[v[i]] = a+c*d/(2*b); t[v[i]] = b+c*d/(2*a); s[v[i+1]] = c+c/2; t[v[i+1]] = d+d/2; ot[v[i+1]]++; wnx.push_back(v[i]); if(ot[v[i+1]] < m){ lnx.push_back(v[i+1]); } } else{ s[v[i+1]] = c+a*b/(2*d); t[v[i+1]] = d+a*b/(2*c); s[v[i]] = a+a/2; t[v[i]] = b+b/2; ot[v[i]]++; wnx.push_back(v[i+1]); if(ot[v[i]] < m){ lnx.push_back(v[i]); } } } if(v.size() %2 == 1){ wnx.push_back(v[v.size()-1]); } v.clear(); for(int i = 0; i < wnx.size();i++){ v.push_back(wnx[i]); } wnx.clear(); for(int i = 0; i < lnx.size();i++){ v.push_back(lnx[i]); } lnx.clear(); } cout <<v[0] << endl; return 0; } ``` ::: - [ ] 3. [動線安排](https://zerojudge.tw/ShowProblem?problemid=g596) :warning: (應該是最複雜的考古題,這題會了可以開始學dfs bfs之類的走訪類型的演算法) - [ ] 4. [魔王迷宮](https://zerojudge.tw/ShowProblem?problemid=g276) :::spoiler c++(二維vector的部分 把它當陣列) ```cpp #include <bits/stdc++.h> using namespace std; struct demon { bool alive = true; int r, c, s, t; }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m, k; cin >> n >> m >> k; vector<demon> d(k); vector<vector<bool>> kill(n, vector<bool> (m)); vector<vector<bool>> bomb(n, vector<bool> (m)); for (int i = 0; i < k; i++) cin >> d[i].r >> d[i].c >> d[i].s >> d[i].t; int alive = k; while (alive) { for (int i = 0; i < k; i++) { if (d[i].alive == false) continue; int x = d[i].r, y = d[i].c; bomb[x][y] = true; } for (int i = 0; i < k; i++) { if (d[i].alive == false) continue; int nx = d[i].r + d[i].s; int ny = d[i].c + d[i].t; if (nx >= n || nx < 0 || ny >= m || ny < 0) { d[i].alive = false; alive--; } else if (bomb[nx][ny]) { d[i].alive = false; alive--; kill[nx][ny] = true; } else { d[i].r = nx, d[i].c = ny; } } for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (kill[i][j]) bomb[i][j] = false, kill[i][j] = false; } int ans = 0; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (bomb[i][j]) ans++; cout << ans << endl; return 0; } ``` ::: - [X] 5. [流量](https://zerojudge.tw/ShowProblem?problemid=f606) :::spoiler c++ ```cpp 我題目看不懂:) ``` ::: - [ ] 6. [人口遷移](https://zerojudge.tw/ShowProblem?problemid=f313) :::spoiler c++ ```cpp #include<bits/stdc++.h> using namespace std; int mv[][2] = {{1,0},{-1,0},{0,1},{0,-1}}; int a[55][55]; int b[55][55]; int d[55][55]; int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int r,c,k,m; memset(a,-1,sizeof(a)); memset(b,0,sizeof(b)); memset(d,0,sizeof(d)); cin >> r >> c >> k >> m; for(int i = 1; i <= r;i++){ for(int j = 1;j<=c;j++ ){ cin >> a[i][j]; } } for(int z = 0;z<m;z++){ for(int i = 1; i <= r;i++){ for(int j = 1;j<=c;j++ ){ if(a[i][j]==-1 ){ continue; } for(int l = 0;l <4;l++){ int x = i + mv[l][0],y = j+mv[l][1]; if(a[x][y]!=-1){ b[x][y]+=a[i][j]/k; d[i][j]+=a[i][j]/k; } } } } for(int i = 1; i <= r;i++){ for(int j = 1;j<=c;j++ ){ a[i][j]-=d[i][j]; a[i][j]+=b[i][j]; } } memset(d,0,sizeof(d)); memset(b,0,sizeof(b)); } int max = -1; int min = 101; for(int i = 1; i <= r;i++){ for(int j = 1;j<=c;j++ ){ if(a[i][j]>max &&a[i][j]!=-1){ max = a[i][j]; } if(a[i][j]<min&& a[i][j]!=-1){ min = a[i][j]; } } } cout << min << endl<< max << endl; return 0; } ``` ::: - [ ] 7. [骰子](https://zerojudge.tw/ShowProblem?problemid=f580) - [ ] 8. [矩陣總和](https://zerojudge.tw/ShowProblem?problemid=h027) :::spoiler c++ ```cpp #include <bits/stdc++.h> using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); int s,t,n,m,r; cin >> s >> t >> n >> m >> r; int a[s][t]; int b[n][m]; int min = 30000; int as= 0; for(int i = 0;i < s;i++){ for(int j = 0;j <t;j++){ cin >> a[i][j]; as+=a[i][j]; } } for(int i = 0;i < n;i++){ for(int j = 0;j <m;j++){ cin >> b[i][j]; } } int ans = 0; for(int i = 0;i < n-s+1;i++){ for(int j = 0;j < m-t+1;j++){ int cnt = 0; int sum = 0; for(int k = 0;k <s;k++){ for(int l = 0; l < t;l++){ if(a[k][l] !=b[k+i][l+j]){ cnt++; } sum +=b[k+i][l+j]; } } if(cnt<=r){ ans++; int d = abs(as-sum); if(d < min){ min = d; } } } } cout << ans << endl; if(ans == 0){ cout << -1 << endl; } else{ cout << min << endl; } return 0; } ``` ::: - [ ] 9. [機器人的路徑](https://zerojudge.tw/ShowProblem?problemid=e287) :::spoiler c++ ```cpp #include <bits/stdc++.h> using namespace std; int a[105][105]; bool v[105][105]; int s = 0; int mv[][2] ={{0,1},{1,0},{0,-1},{-1,0}}; int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); memset(v,0,sizeof(v)); memset(a,0,sizeof(a)); int n,m; cin >> n >> m; int min = 1000000; int x,y; for(int i = 0; i < n;i++){ for(int j = 0; j < m;j++){ cin >>a[i][j]; if(min >a[i][j]){ min = a[i][j]; x= i; y = j; } } } v[x][y]=1; s+=a[x][y]; while(1){ vector<int> dr ; for(int i = 0; i < 4;i++){ int dx = x+mv[i][0],dy = y+mv[i][1]; if(dx>=0 and dx <n and dy>=0 and dy<m){ if(v[dx][dy] == 0){ dr.push_back(a[dx][dy]); } } } if(dr.empty()){ break; } else{ sort(dr.begin(),dr.end()); for(int i = 0; i < n;i++){ for(int j = 0; j < m;j++){ if(a[i][j] == dr[0]){ x = i; y = j; break; } } } s+=a[x][y]; v[x][y] = 1; } } cout << s << endl; return 0; } ``` ::: - [X] 12. [完全奇數](https://zerojudge.tw/ShowProblem?problemid=e294) (類似題) - [ ] 13. [交錯字串](https://zerojudge.tw/ShowProblem?problemid=c462) :::spoiler c++(分段法) ```cpp #include<bits/stdc++.h> using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); string s; int k; vector<int>v; cin >> k >> s; int c = 1; for(int i= 1; i < s.size();i++){ if(isupper(s[i])== isupper(s[i-1])){ c++; } else{ v.push_back(c); c = 1; } } v.push_back(c); int su = 0; vector<int>sol; for(int i = 0; i < v.size();i++){ if(v[i]==k){ su++; } else{ if(i-su-1>=0&&v[i-su-1] >k){ su++; } if(v[i]>k){ su++; } sol.push_back(su); su = 0; } } if(su>0){ if(v[v.size()-1-su] >k){ su++; } sol.push_back(su); } sort(sol.begin(),sol.end()); cout << sol[sol.size()-1]*k << endl; return 0;} ``` ::: - [ ] 14. [小群體](https://zerojudge.tw/ShowProblem?problemid=c291) :::spoiler c++ ```cpp #include <bits/stdc++.h> using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int a; cin >> a; int b[a]; for(int i = 0; i < a;i++){ cin >> b[i]; } int s = 0; bool vv[a]; bool vvs[a]; memset(vvs,0,sizeof(vvs)); memset(vv,0,sizeof(vv)); for(int i = 0; i < a;i++){ if(vvs[i] ==1){ continue; } else{ s++; int x = b[i]; vvs[i] = 1; while(vv[i] != 1){ if(b[x] == b[i]){ vv[i] = 1; } else{ vvs[x] =1; x = b[x]; } } } } cout << s << endl; return 0;} ``` ::: - [ ] 15. [最大和](https://zerojudge.tw/ShowProblem?problemid=c295) :::spoiler c++ sol1 ```cpp #include <bits/stdc++.h> using namespace std; int main(){ ios_base::sync_with_stdio(0); cin.tie(nullptr); int n,m; cin >> n >> m; int sum = 0; vector<int> x; int a[m]; while(n--){ for(int i = 0; i < m;i++){ cin >> a[i]; } int mx = 0; for(int i = 0; i < m; i++){ if( a[i]>mx ){ mx = a[i]; } } x.push_back(mx); sum+=mx; } cout << sum << endl; int count = 0; for(int i = 0; i < x.size();i++){ if(sum%x[i] == 0){ count+=1; } } if(count == 0){ cout << -1; } int y =0; for(int i = 0; i < x.size();i++){ if(sum%x[i] == 0){ cout << x[i]; if(y != count-1){ cout << " "; y++; } } } return 0; } ``` ::: :::spoiler c++ sol2 ```cpp #include <bits/stdc++.h> using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int m,n; cin >> m >>n; vector<int>v; for(int i = 0; i < m;i++){ int a[n]; for(int j = 0; j< n;j++){ cin >> a[j]; } sort(a,a+n); v.push_back(a[n-1]); } int s = 0; for(int i=0; i < v.size();i++){ s+=v[i]; } cout << s<< endl; vector<int>vv; for(int i =0 ;i < v.size();i++){ if(s%v[i] == 0 ){ vv.push_back(v[i]); } } if(vv.empty()){ cout << -1<< endl; } else{ for(int i=0; i < vv.size();i++){ if(i != 0){ cout <<" " ; } cout << vv[i]; } } return 0; } ``` ::: :::spoiler Python ```python n,m=map(int,input().split()) l=[] for i in range(N): l.append(max([int(x) for x in input().split()])) su=sum(l) print(su) l2=[] for i in l: if su%i==0: l2.append(i) if bool(l2): print(*l2) else: print(-1) ``` ::: - [ ] 16. [矩陣轉換](https://zerojudge.tw/ShowProblem?problemid=b965) :::spoiler c++ ```cpp #include <bits/stdc++.h> int a[50][50]; int b[10][10]; using namespace std; int main(){ int r,c,k; cin >> r >> c >> k; for(int i= 0; i < r;i++){ for(int j = 0; j < c;j++){ cin >> a[i][j]; } } int xx[k]; for(int x = 0; x < k;x++){ cin >> xx[x]; } for(int x = k-1; x >= 0;x--){ if(xx[x] == 1){ for(int i= 0; i <r/2;i++){ for(int j = 0; j < c;j++){ swap(a[i][j],a[r-1-i][j]); } } } if(xx[x]== 0){ for(int i = 0; i < r;i++){ for(int j = 0; j< c;j++){ b[i][j] =a[i][j]; } } swap(r,c); for(int i = 0; i < r;i++){ for(int j = 0; j< c;j++){ a[i][j] = b[j][r-i-1]; } } } } cout<< r <<" " <<c << endl; for(int i = 0; i < r;i++){ for(int j = 0; j< c;j++) { if(j !=0){ cout << " "; } cout << a[i][j]; } cout << endl; } return 0; } ``` ::: - [ ] 17. [運貨站](https://zerojudge.tw/ShowProblem?problemid=j123):warning:(tips:如果很多東西要寫在main裡會變得很亂的話,可以把每個都變成函式,會清楚很多喔~) :::spoiler c++ ```cpp #include <bits/stdc++.h> using namespace std; int r, c; int pt = 0; bool v[35][55]; int cnt; void A(int j){ int i = c; for(;i >= 0;i--){ if(i == 0 ||v[j][i-1] || v[j+1][i-1] || v[j+2][i-1] || v[j+3][i-1]) break; } if(i >= c){ cnt ++; } else { v[j][i] =1; v[j+1][i] = 1; v[j+2][i] = 1; v[j+3][i] = 1; pt+=4; } } void B(int j){ int i = c+2; for(;i >= 2;i--){ if(i == 2 || v[j][i-3]) break; } if(i >= c){ cnt++; } else { v[j][i] = 1; v[j][i-1] = 1; v[j][i-2] = 1; pt+=3; } } void C(int j){ int i = c+1; for(;i >= 1;i--){ if(i == 1 || v[j][i-2] || v[j+1][i-2]) break; } if(i >= c) {cnt ++; } else { v[j][i] = 1; v[j+1][i] =1; v[j][i-1] =1; v[j+1][i-1] = 1; pt+=4; } } void D(int j){ int i = c+2; for(;i >= 2;i--){ if(i == 2 || v[j+1][i-3] || v[j][i-1]) break; } if(i >= c){ cnt++; } else { v[j+1][i] = 1; v[j+1][i-1] = 1; v[j+1][i-2] = 1; v[j][i] = 1; pt+=4; } } void E(int j){ int i = c+1; for(;i >= 1;i--){ if(i == 1 || v[j][i-1] ||v[j+1][i-2] || v[j+2][i-2]) break; } if(i >= c){ cnt++; } else { v[j][i] = 1; v[j+1][i] = 1; v[j+2][i] = 1; v[j+1][i-1] = 1; v[j+2][i-1] = 1; pt+=5; } } int main(){ int n; cin >> r >> c >> n; char t[n]; int y[n]; for(int i = 0; i < n;i++){ cin >> t[i] >>y[i]; if(t[i] == 'A') { A(y[i]); } if(t[i] == 'B') { B(y[i]); } if(t[i] == 'C') { C(y[i]); } if(t[i] == 'D') { D(y[i]); } if(t[i] == 'E') { E(y[i]); } } cout << r*c-pt << ' ' << cnt << endl; return 0; } ``` ::: - [ ] 18. [造字程式](https://zerojudge.tw/ShowProblem?problemid=j606) :::spoiler c++ ```cpp #include<bits/stdc++.h> using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int k,q,r; cin >> k >> q>> r; char ch[q+1][k]; cin >> ch[0]; for(int i= 1; i <= q;i++){ for(int j = 0; j < k;j++){ int s ; cin >> s; ch[i][s-1] =ch[i-1][j]; } } for(int i = 0;i <r;i++){ for(int j = 1; j <=q;j++ ){ cout << ch[j][i]; } cout << endl; } return 0; } ``` ::: - [ ] 19. [特殊位置](https://zerojudge.tw/ShowProblem?problemid=k732) :::spoiler c++ ```cpp= #include <bits/stdc++.h> using namespace std; int a[55][55]; int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int n,m; cin >> n >> m; vector<pair<int,int>>v; for(int i = 0; i < n;i++){ for(int j = 0; j < m;j++){ cin >> a[i][j]; } } for(int i = 0; i < n;i++){ for(int j = 0; j < m;j++){ int x = a[i][j]; int dx = i,dy = j; int s = 0; for(int k = 0; k < n;k++){ for(int l = 0; l < m;l++){ if((abs(dx-k)+abs(dy-l))<=x){ s+=a[k][l]; } } } if(s%10 == x){ v.push_back({i,j}); } } } cout << v.size() << endl; for(int i = 0; i <v.size();i++){ cout << v[i].first<<" " << v[i].second << endl; } return 0; } ``` ::: :::spoiler Python(40%未修正) ```python n,m = map(int,input().split()) a = [[int(x) for x in input().split()]for _ in range(n)] ft= [] sd =[] for i in range(n): for j in range(m): x = a[i][j] s = 0 for k in range(n): for l in range(m): if (abs(i -k)+abs(j-l))<=x: s+=a[k][l] if s%10 ==x: ft.append(i) sd.append(j) print(len(ft)) for i in range(len(ft)): print(ft[i],sd[i]) ``` ::: :::spoiler Python其他解 ```python n,m=map(int,input().split()) L=[[int(x) for x in input().split()]for _ in range(n)] result=[] for i in range(n): for j in range(m): count=0 for s in range(i-L[i][j],i+L[i][j]+1): dis=L[i][j]-abs(s-i) for t in range(j-dis,j+dis+1): if 0<=s<n and 0<=t<m: count+=L[s][t] if count%10==L[i][j]: result.append([i,j]) print(len(result)) for i in result: print(*i) ``` ::: - [ ] 20. [蜜蜂觀察](https://zerojudge.tw/ShowProblem?problemid=m932) :::spoiler c++ ```cpp #include<bits/stdc++.h> using namespace std; char a[105][105]; int mv[][2] ={{-1,0},{0,1},{1,1},{1,0},{0,-1},{-1,-1}}; int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int n,m,k; cin >> n >> m >> k; memset(a,'*',sizeof(a)); for(int i = 1; i <= n;i++){ for(int j = 1; j <= m;j++){ cin >>a[i][j]; } } string s; int x = n,y = 1; while(k--){ int t; cin >> t; int dx = x+mv[t][0]; int dy = y+mv[t][1]; if(a[dx][dy]=='*'){ s+=a[x][y]; } else{ s+=a[dx][dy]; x = dx; y = dy; } } cout << s << endl; set<char>ss; for(int i = 0; i < s.size();i++){ ss.insert(s[i]); } cout << ss.size() << endl; return 0; } ``` ::: ::: spoiler Python ```python m,n,k = map(int,input().split()) a = [[0]*n for i in range(m)] for i in range(m): s = list(input()) for j in range(n): a[i][j] = s[j] mv = list(map(int,input().split())) def mov(mv, x,y): if mv == 0: return x + 0, y - 1 elif mv == 1: return x + 1, y + 0 elif mv == 2: return x + 1, y + 1 elif mv == 3: return x + 0, y + 1 elif mv == 4: return x - 1, y + 0 elif mv == 5: return x - 1, y - 1 x= 0 y = m-1 ans = [] for i in range(k): movv = mv[i] xx = x yy= y x , y = mov(movv,x,y) if x >= 0 and x < n and y >=0 and y <m: ans.append(a[y][x]) else: x = xx y = yy ans.append(a[y][x]) print("".join(ans)) print(len(set(ans))) ``` ::: - [ ] 21. [卡牌遊戲](https://zerojudge.tw/ShowProblem?problemid=m371) :::spoiler c++ ```cpp #include <bits/stdc++.h> using namespace std; int main(){ int n,m; cin >> n >> m; int a[n][m]; for(int i = 0; i < n;i++){ for(int j = 0; j < m;j++){ cin >> a[i][j]; } } int s = 0; for(int z = 0;z<n*m/2+1;z++){ for(int i = 0; i < n;i++){ for(int j = 0; j < m;j++){ if(a[i][j] == -1){ continue; } for(int k =j+1; k <m;k++ ){ if(a[i][k] == -1){ continue; } if(a[i][k]==a[i][j]){ s+=a[i][k]; a[i][k] = a[i][j] = -1; break; } else{ break; } } for(int k =i+1; k <n;k++ ){ if(a[k][j] == -1){ continue; } if(a[i][j]==a[k][j]){ s+=a[k][j]; a[k][j] = a[i][j] = -1; break; } else{ break; } } } } } cout << s << endl; return 0; } ``` ::: :::spoiler Python ```python n,m = map(int,input().split()) a =[] for i in range (n): a.append([int(y) for y in input().split()]) pt = 0 k = int(m*n/2) for z in range(0,k): for i in range(n): for j in range (m): if a[i][j] != -1: for l in range(j+1,m): if a[i][l] != -1 and a[i][l]!=a[i][j]: break; elif a[i][l] == -1: continue else: pt +=a[i][j] a[i][j] = -1 a[i][l] = -1 for l in range(i+1,n): if a[l][j] != -1 and a[l][j]!=a[i][j]: break; elif a[l][j] == -1: continue else: pt +=a[i][j] a[i][j] = -1 a[l][j] = -1 print(pt) ``` ::: - [ ] 22. [黑洞旅行](https://apcs-simulation.com/problem/apcs0402) (模測團隊題) :::spoiler c++ ```cpp #include <bits/stdc++.h> using namespace std; char a[9][9]; int mv[][8] = {{-1,0},{-1,1},{0,1},{1,1},{1,0,},{1,-1},{0,-1},{-1,-1}}; string ss ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; int px(int x,int t){ int nx = x+mv[t][0]; if(nx == -1){ nx = 7; } if(nx ==8){ nx = 0; } return nx; } int py(int y,int t){ int ny = y+mv[t][1]; if(ny==-1){ ny = 7; } if(ny==8){ ny = 0; } return ny; } int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int p = 0; int n; cin >> n; for(int i = 0;i <8;i++){ for(int j = 0; j <8;j++){ a[i][j] = ss[p++]; } } while(n--){ char s; int t; int N; cin >> s >> t >> N; int x,y; for(int i = 0;i <8;i++){ for(int j = 0; j <8;j++){ if(a[i][j] == s){ x =i; y =j; break; } } } for(int i = 1; i<=N;i++){ for(int j = 0; j <i;j++){ int nx = px(x,t); int ny = py(y,t); x = nx; y = ny; } t++; if(t == 8){ t =0; } } cout << a[x][y] << endl; } return 0; } ``` ::: :::spoiler Python ```python s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" f = ((-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)) for _ in range(int(input())): a,b,c = input().split() p = s.find(a) x,y = divmod(p,8) i = int(b) n = int(c) for j in range(n): dx,dy = f[i%8] x = (x+dx*(j+1))%8 y = (y+dy*(j+1))%8 i+=1 print(s[x*8+y]) ::: ::: spoiler Python一行解 ```python print(*(lambda s,xs,ys,qs:(s[(s.find(a)//8+sum(xs[(int(b)+j)%8]*((j+((int(c)-j-1)//8+1)*4-3)*((int(c)-j-1)//8+1)) for j in range(8)))%8*8+(s.find(a)%8+sum(ys[(int(b)+j)%8]*((j+((int(c)-j-1)//8+1)*4-3)*((int(c)-j-1)//8+1)) for j in range(8)))%8] for a,b,c in qs))("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",(-1,-1,0,1,1,1,0,-1),(0,1,1,1,0,-1,-1,-1),(input().split() for _ in range(int(input())))),sep="\n") ``` ::: - [ ] 23. [電子畫布](https://zerojudge.tw/ShowProblem?problemid=o077) ::: spoiler c++ ```cpp #include <bits/stdc++.h> using namespace std; int a[105][105]; int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int n,m,r; cin >> n >> m >> r; while(r--){ int rr,c,t,x; cin >> rr >> c >> t >> x; for(int i= 0; i < n;i++){ for(int j = 0;j < m;j++){ if((abs(i-rr)+abs(j-c))<=t){ a[i][j]+=x; } } } } for(int i= 0; i < n;i++){ for(int j = 0;j < m;j++){ cout << a[i][j]<<" "; } cout << endl; } return 0; } ``` ::: ::: spoiler Python ```python h,w,n = map(int,input().split()) a = [[0]*20 for _ in range(h)] def draw(r,c,t,x): for i in range(h): for j in range(w): if abs(r-i)+abs(c-j)<= t: a[i][j]+= x for i in range(n): r,c,t,x = map(int,input().split()) draw(r,c,t,x) for i in range(h): for j in range(w): print(a[i][j], end=' ') print() ``` ::: - [ ] 24. [蒐集寶石](https://zerojudge.tw/ShowProblem?problemid=o712) :::spoiler c++(map iterator) ```cpp() #include <bits/stdc++.h> using namespace std; int a[102][102]; int mv[][2]={{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; int gg(int r, int c){ int s = 4; if (a[r][c] == 0){ return 1; } for(int i= 0;i < 4;i++){ if(a[r+mv[i][0]][c+mv[i][1]] ==-1){ s--; } } if(s == 0){ return 1; } return 0; } int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int m,n,k,r,c; memset(a,-1,sizeof(a)); cin >> m >> n >>k>>r>>c; for(int i= 1; i<=m;i++){ for(int j = 1;j <=n;j++){ cin >> a[i][j]; } } r++; c++; int an = 0; int dir = 0; int t = 0; while(1){ if(gg(r,c)){ break; } an+=a[r][c]; t++; a[r][c]--; if(an%k == 0){ dir = (dir+1)%4; } int tr,tc; while (1) { tr = r + mv[dir][0]; tc = c + mv[dir][1]; if (a[tr][tc] == -1) dir = (dir + 1) % 4; else break; } r = tr; c = tc; } cout << t; return 0; } ``` ::: - [ ] 25. [轉盤得分](https://zerojudge.tw/ShowProblem?problemid=q837) :::spoiler c++ ```cpp #include<bits/stdc++.h> using namespace std; string rotate_wheel(string s, int rotation) { int n = s.size(); rotation %= n; if (rotation < 0) { rotation += n; } return s.substr(n - rotation) + s.substr(0, n - rotation); } int main() { int m, n, k; cin >> m >> n >> k; vector<string> wheels(m); for (int i = 0; i < m; ++i) { cin >> wheels[i]; } long long total_score = 0; for (int i = 0; i < k; ++i) { vector<int> rotations(m); for (int j = 0; j < m; ++j) { cin >> rotations[j]; } for (int j = 0; j < m; ++j) { wheels[j] = rotate_wheel(wheels[j], rotations[j]); } int round_score = 0; for (int col = 0; col < n; ++col) { map<char, int> char_counts; for (int row = 0; row < m; ++row) { char_counts[wheels[row][col]]++; } int max_count = 0; for (auto const& pair : char_counts) { if (pair.second > max_count) { max_count = pair.second; } } round_score += max_count; } total_score += round_score; } cout << total_score << endl; return 0; } ``` ::: - [ ] 26. [字串操作](https://zerojudge.tw/ShowProblem?problemid=q182) :::spoiler c++ ```cpp #include <bits/stdc++.h> using namespace std; string s; int main(){ cin >> s; int x; cin >> x; for(int i = 0; i < x;i++){ int y; cin >> y; if(y == 0){ for(int j = 0;j < s.size();j+=2){ swap(s[j],s[j+1]); } } else if(y == 1){ for(int j = 0;j < s.size();j+=2){ if((s[j]-'0')>(s[j+1]-'0')){ swap(s[j],s[j+1]); } } } else{ string b = s.substr(s.size()/2); string c = s.substr(0,s.size()/2); s.clear(); for(int j =0;j < b.size();j++){ s+=c[j]; s+=b[j]; } } } cout << s <<endl; return 0;} ``` :::

Import from clipboard

Paste your markdown or webpage here...

Advanced permission required

Your current role can only read. Ask the system administrator to acquire write and comment permission.

This team is disabled

Sorry, this team is disabled. You can't edit this note.

This note is locked

Sorry, only owner can edit this note.

Reach the limit

Sorry, you've reached the max length this note can be.
Please reduce the content or divide it to more notes, thank you!

Import from Gist

Import from Snippet

or

Export to Snippet

Are you sure?

Do you really want to delete this note?
All users will lose their connection.

Create a note from template

Create a note from template

Oops...
This template has been removed or transferred.
Upgrade
All
  • All
  • Team
No template.

Create a template

Upgrade

Delete template

Do you really want to delete this template?
Turn this template into a regular note and keep its content, versions, and comments.

This page need refresh

You have an incompatible client version.
Refresh to update.
New version available!
See releases notes here
Refresh to enjoy new features.
Your user state has changed.
Refresh to load new user state.

Sign in

Forgot password

or

By clicking below, you agree to our terms of service.

Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
Wallet ( )
Connect another wallet

New to HackMD? Sign up

Help

  • English
  • 中文
  • Français
  • Deutsch
  • 日本語
  • Español
  • Català
  • Ελληνικά
  • Português
  • italiano
  • Türkçe
  • Русский
  • Nederlands
  • hrvatski jezik
  • język polski
  • Українська
  • हिन्दी
  • svenska
  • Esperanto
  • dansk

Documents

Help & Tutorial

How to use Book mode

Slide Example

API Docs

Edit in VSCode

Install browser extension

Contacts

Feedback

Discord

Send us email

Resources

Releases

Pricing

Blog

Policy

Terms

Privacy

Cheatsheet

Syntax Example Reference
# Header Header 基本排版
- Unordered List
  • Unordered List
1. Ordered List
  1. Ordered List
- [ ] Todo List
  • Todo List
> Blockquote
Blockquote
**Bold font** Bold font
*Italics font* Italics font
~~Strikethrough~~ Strikethrough
19^th^ 19th
H~2~O H2O
++Inserted text++ Inserted text
==Marked text== Marked text
[link text](https:// "title") Link
![image alt](https:// "title") Image
`Code` Code 在筆記中貼入程式碼
```javascript
var i = 0;
```
var i = 0;
:smile: :smile: Emoji list
{%youtube youtube_id %} Externals
$L^aT_eX$ LaTeX
:::info
This is a alert area.
:::

This is a alert area.

Versions and GitHub Sync
Get Full History Access

  • Edit version name
  • Delete

revision author avatar     named on  

More Less

Note content is identical to the latest version.
Compare
    Choose a version
    No search result
    Version not found
Sign in to link this note to GitHub
Learn more
This note is not linked with GitHub
 

Feedback

Submission failed, please try again

Thanks for your support.

On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

Please give us some advice and help us improve HackMD.

 

Thanks for your feedback

Remove version name

Do you want to remove this version name and description?

Transfer ownership

Transfer to
    Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

      Link with GitHub

      Please authorize HackMD on GitHub
      • Please sign in to GitHub and install the HackMD app on your GitHub repo.
      • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
      Learn more  Sign in to GitHub

      Push the note to GitHub Push to GitHub Pull a file from GitHub

        Authorize again
       

      Choose which file to push to

      Select repo
      Refresh Authorize more repos
      Select branch
      Select file
      Select branch
      Choose version(s) to push
      • Save a new version and push
      • Choose from existing versions
      Include title and tags
      Available push count

      Pull from GitHub

       
      File from GitHub
      File from HackMD

      GitHub Link Settings

      File linked

      Linked by
      File path
      Last synced branch
      Available push count

      Danger Zone

      Unlink
      You will no longer receive notification when GitHub file changes after unlink.

      Syncing

      Push failed

      Push successfully