首页 > 试题广场 >

重排数列

[编程题]重排数列
  • 热度指数:11003 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 98M,其他语言196M
  • 算法知识视频讲解
小易有一个长度为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。
示例1

输入

2
3
1 10 100
4
1 2 3 4

输出

Yes
No
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = Integer.parseInt(sc.nextLine());
        List<int[]> list = new ArrayList<>();
        String[] result = new String[t];
        for(int i = 0;i<t;i++){
            int num = sc.nextInt();
            int[] tt = new int[num];
            for(int j = 0;j<num;j++){
                tt[j] = sc.nextInt();
            }
            list.add(tt);
        }
        for(int i = 0;i<list.size();i++){
            int[] flag = new int[list.get(i).length];
            for(int j =0;j<list.get(i).length;j++){
                if(list.get(i)[j]%2==0){
                    flag[j] = 1;
                }
                if(list.get(i)[j]%4==0){
                    flag[j] = 2;
                }
            }
            int ff=0;
            for(int k = 0;k<flag.length;k++){
                ff += flag[k];
            }
            if(ff>=flag.length){
                result[i] = "Yes";
            }
            else {
                result[i] = "No";
            }
        }
        for (String rr:result
             ) {
            System.out.println(rr);
        }

    }
}
思路就是2的倍数为1,4的倍数为2,所有值加起来要大于等于长度
发表于 2022-02-12 21:39:18 回复(0)
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;
        }
    }
}
发表于 2021-04-21 14:25:51 回复(0)
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的倍数的数大于或等于包含奇数的数量
        }
    }
}


发表于 2019-07-03 12:00:16 回复(0)

内存超限

求大佬讲解。。。感觉这样并不会占用太多内存呀,怎么就超了?只通过80%用例。

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();    
    }    
}
编辑于 2017-11-23 22:33:42 回复(0)