給定N個正整數,請統計奇數和偶數各有多少個?
輸入格式:
輸入第一行給出一個正整N(<= 1000);第2行給出N個正整數,以空格分隔。
輸出格式:
在一行中先後輸出奇數的個數、偶數的個數。中間以1個空格分隔。
輸入樣例:
9
88 74 101 26 15 0 34 22 77
輸出樣例:
3 6
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
int len1 = 0, len2 = 0; // len1-奇數個數,len2-偶數個數
while (N-- > 0) {
int num = scanner.nextInt();
// if (num % 2 == 0)
// 與運算,最後一位和 1 相與,偶數相與結果爲 0
if ((num & 1) == 0) {
len2++;
} else {
len1++;
}
}
System.out.println(len1 + " " + len2);
}
}