解题思路:
注意事项:字符输入—scanf(" %c")(空格可换成\n \t),单个字符读取,可忽略掉空格、换行
参考代码:
#include <stdio.h> //查找周围雷的数量 int mine_count(char str[100][100], int x,int y) { int count = 0; for (int i = x - 1; i <=x + 1; i++) { for (int j = y - 1; j <=y + 1; j++) { if (str[i][j] == '*')//有雷加1 { count++; } } } return count; } int main() { char str[100][100]={0,0}; int n = 0, m = 0; int k = 0; while (~scanf("%d%d", &n, &m)) { if (n == m && n == 0)//结束标志 { break; } k++; for (int i = 1; i <=n; i++) { for (int j = 1; j <=m; j++) { scanf(" %c",&str[i][j]);//单个字符读取 } } printf("Field #%d:\n", k); //输出雷和雷数 for (int i = 1; i <=n; i++) { for (int j = 1; j <=m; j++) { if (str[i][j] == '*') { printf("%c", str[i][j]); } else { int count = 0; count = mine_count(str, i, j); printf("%d", count); } } printf("\n"); } printf("\n");//满足格式,输出后换行 } }
0.0分
1 人评分
字符串的输入输出处理 (C语言代码)浏览:1021 |
A+B for Input-Output Practice (V) (C语言代码)浏览:640 |
C语言程序设计教程(第三版)课后习题5.6 (C语言代码)浏览:913 |
幸运数 (C++代码)浏览:1309 |
循环入门练习6 (C语言代码)浏览:1058 |
C语言程序设计教程(第三版)课后习题11.1 (C语言代码)浏览:525 |
明明的随机数 (C语言代码)浏览:965 |
陶陶摘苹果2 (C++代码)浏览:578 |
C语言程序设计教程(第三版)课后习题3.7 (C语言代码)浏览:611 |
简单的a+b (C语言代码)浏览:491 |