###### tags: `APCS` `c++` `陣列` # 題解 zerojudge f313: 2. 人口遷移 APCS ```cpp= #include<bits/stdc++.h> using namespace std; int main(){ ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0); int r,c,k,m; cin>>r>>c>>k>>m; int city[55][55]; for(int i=0;i<55;i++) for(int j=0;j<55;j++)city[i][j]=-1;//預設全部都非城市 for(int i=1;i<=r;i++){ for(int j=1;j<=c;j++) cin>>city[j][i]; } int mov; while(m--){ int mov_c[55][55]={0}; for(int i=1;i<=r;i++){ for(int j=1;j<=c;j++){ mov=city[j][i]/k;//紀錄移動人數 if(city[j][i]>=0){ if(city[j-1][i]>=0){mov_c[j-1][i]+=mov;mov_c[j][i]-=mov;} if(city[j+1][i]>=0){mov_c[j+1][i]+=mov;mov_c[j][i]-=mov;} if(city[j][i-1]>=0){mov_c[j][i-1]+=mov;mov_c[j][i]-=mov;} if(city[j][i+1]>=0){mov_c[j][i+1]+=mov;mov_c[j][i]-=mov;} } } } for(int i=1;i<=r;i++){ for(int j=1;j<=c;j++) city[j][i]+=mov_c[j][i];//變更城市人數 } } int mini=10000,maxi=0; for(int i=1;i<=r;i++){ for(int j=1;j<=c;j++){ maxi=max(maxi,city[j][i]); if(city[j][i]>=0) mini=min(mini,city[j][i]); } } cout<<mini<<'\n'<<maxi; } ```
×
Sign in
Email
Password
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