Try   HackMD

考古題

  • 以後會持續更新參考程式碼(由於有人嫌我的code太暴力所以只好全部重寫
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    )
    盡量要練到對一題半以上,這樣才有機會三級分(1題全對+1題測資正確50%)

注意事項

  • 如果程式執行時間超時(TLE),就算測資正確也可能拿不到分數
  • 注意輸出格式要求,多或少空格都會沒有分數
  • 仔細檢查陣列大小,如果超過題目限制大小或太小,都有可能造成錯誤
  • 記得做 i/o 優化 程式碼如下(背一下,有時能卡時間),以下優化前後差別
  • 詳解先不要著急點開,練習的時候會思考很多天都是很正常的
    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →
i/o優化
//記得要寫在main()裡
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
  • 解不出來能暴力就暴力,只要子題組正確率越高就越有機會增加級分
  • 三四題如果還有時間的話可以嘗試暴力或印測資(以確保拿到前兩題為前提)

二級分考古題

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
:較難

c++
#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;
}
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}")

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)

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. 七言對聯
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
c++
#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;
}

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="")
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)
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)

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. 猜拳
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
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)
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. 邏輯運算子
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
  • 11. 秘密差
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))

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")
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")

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)
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. 路徑偵測
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
c++
#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;
}

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)
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)


c++
#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; 
}
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])
        

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])


c++
#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;
}

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)
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))

三級分考古題 二維陣列很重要
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

c++
#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;}
c++
#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. 動線安排
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    (應該是最複雜的考古題,這題會了可以開始學dfs bfs之類的走訪類型的演算法)
  • 4. 魔王迷宮
c++(二維vector的部分 把它當陣列)
#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;
}
c++
我題目看不懂:)
c++
#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;
}
c++
#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;
}
c++
#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;
}
c++(分段法)
#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;}

c++
#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;}

c++ sol1
#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;
}
c++ sol2
#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;
}
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)

 


c++
#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. 運貨站
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
c++
#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;
}
c++
#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;
}
c++
#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; }
Python(40%未修正)
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])

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)
c++
#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;
}

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)))

c++
#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;
}
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)
                            
                            

                
c++
#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;
}
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])

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")
c++
#include <bits/stdc++.h>
using namespace std;
int a[105][105];
int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  memset(a,0,sizeof(a));
  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;
}

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()