PAT

    技术2025-06-25  10

    这个题目在最后一个测试点有一个小坑,就是判断A2是否为零的情况有两种可能: (1)没有符合条件的数,A2始终为0; (2)进行至少两步的交错相加,结果为0. 一开始我没有想到第二种情况导致最后一个测试点一直出错,后添加了一个变量a2对上面两种情况进行判断,得以全部通过。

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); String[] str = bf.readLine().split(" "); int[] max = new int[str.length]; int A1 = 0, A2 = 0, A3 = 0, A4 = 0, A5 = 0, a2 = 0; double sum = 0; int flag = 1; for (int i = 1; i < str.length; i++) { if (Integer.parseInt(str[i]) % 5 == 0 && Integer.parseInt(str[i]) % 2 == 0) { A1 += Integer.parseInt(str[i]); } else if (Integer.parseInt(str[i]) % 5 == 1) { a2++; A2 += flag * Integer.parseInt(str[i]); flag = -flag; } else if (Integer.parseInt(str[i]) % 5 == 2) { A3++; } else if (Integer.parseInt(str[i]) % 5 == 3) { sum += Integer.parseInt(str[i]); A4++; } else if (Integer.parseInt(str[i]) % 5 == 4) { max[A5++] = Integer.parseInt(str[i]); } } //A1 if (A1 == 0) { System.out.print("N "); } else { System.out.print(A1 + " "); } //A2 if (A2 == 0 && a2 == 0) { System.out.print("N "); } else { System.out.print(A2 + " "); } //A3 if (A3 == 0) { System.out.print("N "); } else { System.out.print(A3 + " "); } //A4 if (A4 == 0) { System.out.print("N "); } else { System.out.print(String.format("%.1f ", sum / A4)); } //A5 if (A5 == 0) { System.out.print("N"); } else { System.out.print(sort(max)); } } public static int sort(int[] max) { int max1 = 0; for (int i = 1; i < max.length; i++) { if (max[i] > max[max1]) max1 = i; continue; } return max[max1]; } }
    Processed: 0.010, SQL: 9