洛谷做题打卡day2
嵌套循环yyds!
很多前辈的题解都很长且包括诸如s[i-1][j-1]一直到s[i+1][j+1]这般冗长的搜索,其实…我觉得直接用循环就好了嘛
两次debug然后AC,嵌套了find函数看起来会稍微简洁一些,其实这题思路不难的——不外乎将字符装进数组,嵌套循环换行,然后分遇‘*’直接输出 ,遇‘?’搜索输出数字两种情况,然后就没有然后了,华丽收场~
#include<iostream>
using namespace std;
char s[110][110];
int t = 0;void find(int a, int b)
{if (s[a][b] == '*') t++;
}int main()
{int n, m, cnt = 0;cin >> n >> m;for (int i = 0; i < n; i++){for (int j = 0; j < m; j++){cin >> s[i][j];}}for (int i = 0; i < n; i++){for (int j = 0; j < m; j++){if (s[i][j] == '*') cout << '*';if (s[i][j] == '?'){for (int p = i - 1; p <= i + 1; p++){for (int q = j - 1; q <= j + 1; q++) find(p, q);}cout << t;t = 0;}}cout << endl;}return 0;
}