Skip to content

L1-022 奇偶分家

给定N个正整数,请统计奇数和偶数各有多少个?

输入格式:

输入第一行给出一个正整N(≤1000);第 2 行给出N个非负整数,以空格分隔。

输出格式:

在一行中先后输出奇数的个数、偶数的个数。中间以 1 个空格分隔。

输入样例:

tex
9
88 74 101 26 15 0 34 22 77
9
88 74 101 26 15 0 34 22 77

输出样例:

tex
3 6
3 6

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));
        int n = Integer.parseInt(in.readLine());
        String[] input = in.readLine().split(" ");
        int odd = 0, even = 0;
        for (int i = 0; i < input.length; i++) {
            int num = Integer.parseInt(input[i]);
            if ((num & 1) == 1) odd++;
            else even++;
        }
        System.out.printf("%d %d", odd, even);
    }
}
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));
        int n = Integer.parseInt(in.readLine());
        String[] input = in.readLine().split(" ");
        int odd = 0, even = 0;
        for (int i = 0; i < input.length; i++) {
            int num = Integer.parseInt(input[i]);
            if ((num & 1) == 1) odd++;
            else even++;
        }
        System.out.printf("%d %d", odd, even);
    }
}

Released under the MIT License.