解题思路:
注意事项:
参考代码:
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { int n1 = 1; Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int n = scanner.nextInt(); int m = scanner.nextInt(); if (n == 0 && m == 0) break; String a[] = new String[n]; char b[][] = new char[n][m]; int c[][] = new int[n][m]; for (int j = 0; j < a.length; j++) { a[j] = scanner.next(); for (int k = 0; k < a[j].length(); k++) { b[j][k] = a[j].charAt(k); if (b[j][k] == '*'&&n!=1) { if (j == 0 && k == 0) { c[j + 1][k]++; c[j][k + 1]++; c[j+1][k + 1]++; } else if (j == 0 && k != 0 && k != c[0].length-1) { c[j][k + 1]++; c[j][k - 1]++; c[j + 1][k]++; c[j+1][k + 1]++; c[j+1][k - 1]++; } else if (j == 0 && k == c[0].length-1) { c[j + 1][k]++; c[j][k - 1]++; c[j+1][k - 1]++; } else if (j != 0 && j != c.length-1 && k == 0) { c[j + 1][k]++; c[j - 1][k]++; c[j][k + 1]++; c[j-1][k + 1]++; c[j+1][k + 1]++; } else if (j != 0 && j != c.length-1 && k == c[j].length-1) { c[j + 1][k]++; c[j - 1][k]++; c[j][k - 1]++; c[j+1][k - 1]++; c[j-1][k - 1]++; } else if (j == c.length-1 && k == 0) { c[j - 1][k]++; c[j][k + 1]++; c[j-1][k + 1]++; } else if (j == c.length-1 && k != 0 && k != c[j].length-1) { c[j - 1][k]++; c[j][k - 1]++; c[j][k + 1]++; c[j-1][k + 1]++; c[j-1][k - 1]++; } else if (j == c.length-1 && k == c[j].length-1) { c[j - 1][k]++; c[j][k - 1]++; c[j-1][k - 1]++; } else { c[j - 1][k]++; c[j - 1][k+1]++; c[j + 1][k]++; c[j - 1][k-1]++; c[j + 1][k+1]++; c[j + 1][k-1]++; c[j][k - 1]++; c[j][k + 1]++; } } if (b[j][k] == '*'&&n==1) { if (j == 0 && k == 0) { c[j][k + 1]++; } else if (j == 0 && k != 0 && k != c[0].length-1) { c[j][k + 1]++; c[j][k - 1]++; } else if (j == 0 && k == c[0].length-1) { c[j][k - 1]++; } } } } System.out.println("Field #" + n1 + ":"); n1++; for (int j = 0; j < c.length; j++) { for (int k = 0; k < c[j].length; k++) { if (b[j][k] == '*') { System.out.print(b[j][k]); } else System.out.print(c[j][k]); } System.out.println(); } System.out.println(); } } }
0.0分
0 人评分
简单的a+b (C语言代码)浏览:641 |
C语言训练-大、小写问题 (C语言代码)浏览:792 |
简单的a+b (C语言代码)浏览:878 |
C语言程序设计教程(第三版)课后习题4.9 (C语言代码)浏览:727 |
C语言训练-字符串正反连接 (C语言代码)浏览:653 |
C二级辅导-计负均正 (C语言代码)浏览:664 |
P1002 (C语言代码)浏览:1028 |
C语言训练-求具有abcd=(ab+cd)2性质的四位数 (C语言代码)浏览:935 |
C语言程序设计教程(第三版)课后习题9.6 (C语言代码)浏览:2755 |
ACM俱乐部密码浏览:948 |