第一题
package ccf201709; import java.util.Scanner; public class p1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); n = n / 10; int count = 0; do { if (n >= 5) { n -= 5; count += 7; } else if (n >= 3) { n -= 3; count += 4; } } while (n >= 3); count += n; System.out.println(count); } }
第二题(使用了动态数组)
package ccf201709; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import static java.util.Collections.sort; 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 k = Integer.parseInt(temp[1]); int[] w = new int[k]; int[] s = new int[k]; int[] c = new int[k]; int[] e = new int[k]; int[][] room = new int[n + 1][2]; for (int i = 1; i < n + 1; i++) { room[i][0] = i; room[i][1] = 1; } for (int i = 0; i < k; i++) { String[] line = bf.readLine().split(" "); w[i] = Integer.parseInt(line[0]); s[i] = Integer.parseInt(line[1]); c[i] = Integer.parseInt(line[2]); e[i] = s[i] + c[i]; } ArrayList<Integer> time = new ArrayList<>(); for (int i = 0; i < k; i++) { if (i == 0) { time.add(s[i]); } if (!time.contains(s[i])) { time.add(s[i]); } if (!time.contains(e[i])) { time.add(e[i]); } } sort(time); for (int i = 0; i < time.size(); i++) { returnKey(time, n, k, i, e, w, room); getKey(time, n, k, i, s, w, room); } for (int i = 1; i < n + 1; i++) { System.out.print(room[i][0] + " "); } } public static void returnKey(ArrayList<Integer> time, int n, int k, int i, int[] e, int[] w, int[][] room) { ArrayList<Integer> retKey = new ArrayList<>(); for (int j = 0; j < k; j++) { if (time.get(i) == e[j]) { retKey.add(w[j]); } } sort(retKey); for (int j = 0; j < retKey.size(); j++) { for (int p = 1; p < n + 1; p++) { if (room[p][1] == 0) { room[p][0] = retKey.get(j); room[p][1] = 1; break; } } } } public static void getKey(ArrayList<Integer> time, int n, int k, int i, int[] s, int[] w, int[][] room) { for (int j = 0; j < k; j++) { if (time.get(i) == s[j]) { for (int p = 1; p < n + 1; p++) { if (room[p][0] == w[j]) { room[p][0] = 0; room[p][1] = 0; break; } } } } } }