第一题
package ccf201909; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class p1 { 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[][] a = new int[n + 1][m + 1]; int[] t = new int[n + 1]; int[] p = new int[n + 1]; for (int i = 1; i <= n; i++) { String[] line = bf.readLine().split(" "); for (int j = 0; j <= m; j++) { a[i][j] = Integer.parseInt(line[j]); if (j == 0) { t[i] = a[i][j]; } else { p[i] += a[i][j]; } } } int resT = 0; int resK = 0; int resP = p[0]; for (int i = 1; i <= n; i++) { resT += t[i] + p[i]; if (resP > p[i]) { resP = p[i]; resK = i; } } resP = resP * (-1); System.out.println(resT + " " + resK + " " + resP); } }
第二题(创建了Arraylist数组,在计算E时要考虑D中是否有重复)
package ccf201909; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; 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)); int n = Integer.parseInt(bf.readLine()); ArrayList<Integer>[] lists = new ArrayList[n + 1]; for (int i = 0; i < lists.length; i++) { lists[i] = new ArrayList<>(); } for (int i = 1; i <= n; i++) { String[] line = bf.readLine().split(" "); int len = Integer.parseInt(line[0]); for (int j = 1; j <= len; j++) { lists[i].add(Integer.parseInt(line[j])); } } long T = 0; ArrayList<Integer> D = new ArrayList<>(); for (int i = 1; i <= n; i++) { long value = lists[i].get(0); for (int j = 1; j < lists[i].size(); j++) { if (lists[i].get(j) > 0) { if (value > lists[i].get(j)) { boolean flag = true; for (int k = 0; k < D.size(); k++) { if (i == D.get(k)) { flag = false; break; } } if (flag) { D.add(i); } } value = lists[i].get(j); } else { value += lists[i].get(j); } } T += value; } sort(D); long E = 0; if (D.size() < 3) { E = 0; } else { if (D.get(0) == 1 && D.get(1) == 2 && D.get(D.size() - 1) == n) { E++; } if (D.get(0) == 1 && D.get(D.size() - 2) == n - 1 && D.get(D.size() - 1) == n) { E++; } for (int i = 1; i < D.size() - 1; i++) { if (D.get(i - 1) == D.get(i) - 1 && D.get(i + 1) == D.get(i) + 1) { E++; } } } System.out.println(T + " " + D.size() + " " + E); } }