第一题
package ccf201512; import java.util.Scanner; public class p1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String n = sc.next(); int count = 0; for (int i = 0; i < n.length(); i++) { count += Integer.parseInt(String.valueOf(n.charAt(i))); } System.out.println(count); } }
第二题
package ccf201512; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class p2 { public static void main(String[] args) throws IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); String[] temp = bf.readLine().split(" "); int n = Integer.parseInt(temp[0]); int m = Integer.parseInt(temp[1]); int[][][] xy = new int[n][m][2]; for (int i = 0; i < n; i++) { String[] line = bf.readLine().split(" "); for (int j = 0; j < m; j++) { xy[i][j][0] = Integer.parseInt(line[j]); xy[i][j][1] = 1; } } for (int i = 0; i < n; i++) { for (int j = 1; j < m - 1; j++) { if (xy[i][j][0] == xy[i][j - 1][0] && xy[i][j][0] == xy[i][j + 1][0]) { xy[i][j - 1][1] = 0; xy[i][j][1] = 0; xy[i][j + 1][1] = 0; } } } for (int j = 0; j < m; j++) { for (int i = 1; i < n - 1; i++) { if (xy[i][j][0] == xy[i - 1][j][0] && xy[i][j][0] == xy[i + 1][j][0]) { xy[i - 1][j][1] = 0; xy[i][j][1] = 0; xy[i + 1][j][1] = 0; } } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (xy[i][j][1] == 0) { System.out.print(0 + " "); } else { System.out.print(xy[i][j][0] + " "); } } System.out.println(); } } }