第一题
package ccf201412; import java.util.Scanner; public class p1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] x = new int[n]; int[] y = new int[n + 1]; int[] res = new int[n]; for (int i = 0; i < n; i++) { boolean flag = false; x[i] = sc.nextInt(); for (int j = 0; j < i; j++) { if (x[j] == x[i]) { flag = true; } } if (flag) { y[x[i]]++; } else { y[x[i]] = 1; } res[i] = y[x[i]]; } for (int i = 0; i < n; i++) { if(i==n-1){ System.out.print(res[i]); }else { System.out.print(res[i]+" "); } } } }
第二题
package ccf201412; import java.util.Scanner; public class p2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[][] xy = new int[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { xy[i][j] = sc.nextInt(); } } int i = 0; int j = 0; System.out.print(xy[0][0] + " "); if (n == 1) { return; } while (true) { if (j == n - 1) { i++; } else { j++; } System.out.print(xy[i][j] + " "); while (i != n - 1 && j != 0) { System.out.print(xy[++i][--j] + " "); } if (i == n - 1) { j++; } else { i++; } System.out.print(xy[i][j] + " "); if (j == n - 1 && i == n - 1) { break; } while (i != 0 && j != n - 1) { System.out.print(xy[--i][++j] + " "); } } } }