第一题
package ccf201409; import java.util.Arrays; import java.util.Scanner; public class p1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] s = new int[n]; int count = 0; for (int i = 0; i < n; i++) { s[i] = sc.nextInt(); } Arrays.sort(s); for (int j = 1; j < n; j++) { if (s[j] - 1 == s[j - 1]) count++; } System.out.println(count); } }
第二题
package ccf201409; import java.util.Scanner; public class p2f { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[][] xy = new int[n][4]; int minX = 1000; int minY = 1000; int maxX = 0; int maxY = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < 4; j++) { xy[i][j] = sc.nextInt(); if (j == 0 || j == 2) { if (xy[i][j] < minX) { minX = xy[i][j]; } if (xy[i][j] > maxX) { maxX = xy[i][j]; } } if (j == 1 || j == 3) { if (xy[i][j] < minY) { minY = xy[i][j]; } if (xy[i][j] > maxY) { maxY = xy[i][j]; } } } } int[][] map = new int[maxX + 1][maxY + 1]; System.out.println(getS(map, xy, maxX, maxY)); } public static int getS(int[][] map, int[][] xy, int maxX, int maxY) { int count = 0; for (int i = 0; i < xy.length; i++) { for (int j = xy[i][1]; j < xy[i][3]; j++) { for (int k = xy[i][0]; k < xy[i][2]; k++) { map[k][j] = 1; } } } for (int j = 0; j <= maxY; j++) { for (int i = 0; i <= maxX; i++) { if (map[i][j] == 1) count++; } } return count; } }