小易有一个长度为N的正整数数列A = {A[1], A[2], A[3]..., A[N]}。
牛博士给小易出了一个难题:
对数列A进行重新排列,使数列A满足所有的A[i] * A[i + 1](1 ≤ i ≤ N - 1)都是4的倍数。
小易现在需要判断一个数列是否可以重排之后满足牛博士的要求。
输入的第一行为数列的个数t(1 ≤ t ≤ 10), 接下来每两行描述一个数列A,第一行为数列长度n(1 ≤ n ≤ 10^5) 第二行为n个正整数A[i](1 ≤ A[i] ≤ 10^9)
对于每个数列输出一行表示是否可以满足牛博士要求,如果可以输出Yes,否则输出No。
2 3 1 10 100 4 1 2 3 4
Yes No
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int times = sc.nextInt();
while (times-- > 0) {
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
boolean res = isValid(arr);
if (res) {
System.out.println("Yes");
} else {
System.out.println("No");
}
}
}
public static boolean isValid(int[] arr) {
int countOdd = 0, countMod2 = 0, countMod4 = 0;
for (int num : arr) {
if (num % 2 == 1) {
countOdd++;
} else {
if (num % 4 != 0) {
countMod2++;
} else {
countMod4++;
}
}
}
if (countMod2 == 0) {
return countMod4 >= countOdd - 1;
} else {
return countMod4 >= countOdd;
}
}
}
public class Main4 {
public static void main(String[] args) {
//预处理数据 可以不保存到内存,我这图个省事。
Scanner sc = new Scanner(System.in);
int count = sc.nextInt();
int[][] arrs = new int[count][];
for (int i = 0; i < arrs.length; i++){
int arrLength = sc.nextInt();
arrs[i] = new int[arrLength];
for(int j = 0; j < arrLength; j++){
arrs[i][j] = sc.nextInt();
}
}
sc.close();
for (int[] arr : arrs){
int counter = 0;
for(int i = 0; i < arr.length; i++){
counter += (((arr[i] & 1) == 1) ? 1 : ((arr[i] & 3) == 0) ? -1 : 0);//是否为奇数如果为奇数+1, 如果能被4整除 -1 , 否则 +0
}
System.out.println(counter <= 0 ? "Yes" : "No");//如果为4的倍数的数大于或等于包含奇数的数量
}
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int arrays = sc.nextInt();//数组数量
int count;//每个数组的长度
int four;//被4整除的数目
int other;//不能被2整除的数目
int temp;//用于临时存数组中的数
for (int j=0;j<arrays;j++) {
count= sc.nextInt();
four = 0;
other = 0;
for (int i = 0; i < count ; i++) {
temp = sc.nextInt();
if ((temp & 3) == 0 )
four++;
else if (0 != (temp & 1) )
other++;
}
if (other > four)
System.out.println("No");
else
System.out.println("Yes");
}
sc.close();
}
}