Skip to content

1012 数字分类

给定一系列正整数,请按要求对数字进行分类,并输出以下 5 个数字:

  • A1 = 能被 5 整除的数字中所有偶数的和;
  • A2 = 将被 5 除后余 1 的数字按给出顺序进行交错求和,即计算 n1−n2+n3−n4⋯;
  • A3 = 被 5 除后余 2 的数字的个数;
  • A4 = 被 5 除后余 3 的数字的平均数,精确到小数点后 1 位;
  • A5 = 被 5 除后余 4 的数字中最大数字。

输入格式:

每个输入包含 1 个测试用例。每个测试用例先给出一个不超过 1000 的正整数 N,随后给出 N 个不超过 1000 的待分类的正整数。数字间以空格分隔。

输出格式:

对给定的 N 个正整数,按题目要求计算 A1~A5 并在一行中顺序输出。数字间以空格分隔,但行末不得有多余空格。

若分类之后某一类不存在数字,则在相应位置输出 N

输入样例 1:

tex
13 1 2 3 4 5 6 7 8 9 10 20 16 18
13 1 2 3 4 5 6 7 8 9 10 20 16 18

输出样例 1:

tex
30 11 2 9.7 9
30 11 2 9.7 9

输入样例 2:

tex
8 1 2 4 5 6 7 9 16
8 1 2 4 5 6 7 9 16

输出样例 2:

tex
N 11 2 N 9
N 11 2 N 9

Solution:

java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String[] input = in.readLine().split(" ");
        int a1 = 0, a2 = 0, a3 = 0, a5 = 0, temp = -1, cnt = 0, flag = 0;
        double a4 = 0;
        for (int i = 1; i < input.length; i++) {
            int num = Integer.parseInt(input[i]);
            if (num % 10 == 0) a1 += num;
            else if (num % 5 == 1) {
                // 交错求和结果可能是0,需要加标志判断
                flag = 1;
                temp *= -1;
                a2 += num * temp;
            } else if (num % 5 == 2) a3++;
            else if (num % 5 == 3) {
                cnt++;
                a4 += num;
            } else if (num % 5 == 4) {
                a5 = Math.max(a5, num);
            }
        }
        System.out.print(a1 > 0 ? a1 : "N");
        System.out.print(flag == 1 ? " " + a2 : " N");
        System.out.print(a3 > 0 ? " " + a3 : " N");
        System.out.print(cnt > 0 ? String.format(" %.1f", a4 / cnt) : " N");
        System.out.print(a5 > 0 ? " " + a5 : " N");
    }
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String[] input = in.readLine().split(" ");
        int a1 = 0, a2 = 0, a3 = 0, a5 = 0, temp = -1, cnt = 0, flag = 0;
        double a4 = 0;
        for (int i = 1; i < input.length; i++) {
            int num = Integer.parseInt(input[i]);
            if (num % 10 == 0) a1 += num;
            else if (num % 5 == 1) {
                // 交错求和结果可能是0,需要加标志判断
                flag = 1;
                temp *= -1;
                a2 += num * temp;
            } else if (num % 5 == 2) a3++;
            else if (num % 5 == 3) {
                cnt++;
                a4 += num;
            } else if (num % 5 == 4) {
                a5 = Math.max(a5, num);
            }
        }
        System.out.print(a1 > 0 ? a1 : "N");
        System.out.print(flag == 1 ? " " + a2 : " N");
        System.out.print(a3 > 0 ? " " + a3 : " N");
        System.out.print(cnt > 0 ? String.format(" %.1f", a4 / cnt) : " N");
        System.out.print(a5 > 0 ? " " + a5 : " N");
    }
}

Released under the MIT License.