题目
思路
题目中说了四周的值不变,那么我们就可以在for循环中用if判断i==0 || i==n-1 || j==0 || j==m-1
我们已知所有数据那么就可以求出来ge个位上的值 就用
round((a[i][j]+a[i-1][j]+a[i+1][j]+a[i][j-1]+a[i][j+1])/5.0)
注 round是四舍五入
代码
#include <iostream>
#include <cmath>
using namespace std;
int main(){
int m,n;
cin>>m>>n;
int a[n][m];
double b[n+2][m+2];
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++){
if(i==0 || i==n-1 || j==0 || j==m-1){
b[i][j]=a[i][j];
}
else {
b[i][j]=round((a[i][j]+a[i-1][j]+a[i+1][j]+a[i][j-1]+a[i][j+1])/5.0);
}
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cout<<b[i][j];
}
}
}