开的数组是200*200
输入从1开始所以没有写越界判定
样例的输出没有问题
前来求助,谢谢大佬
#include <bits/stdc++.h>
using namespace std;
int b[200][200];
char a[200][200];
int main()
{
int n,m;
cin>>n>>m;
memset(b,0,sizeof(b));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>a[i][j];
if(a[i][j]=='*')
{
b[i][j]=-114514;
b[i-1][j-1]++;
b[i-1][j]++;
b[i-1][j+1]++;
b[i][j-1]++;
b[i][j+1]++;
b[i+1][j-1]++;
b[i+1][j]++;
b[i+1][j+1]++;
}
}
}
int jud=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(jud==3)
{
jud=0;
cout<<endl;
}
if(b[i][j]<0)
{
cout<<"*";
}
else
{
cout<<b[i][j];
}
jud++;
}
}
return 0;
}