首页 > 试题广场 >

重排数列

[编程题]重排数列
  • 热度指数:393 时间限制: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.Scanner;


public class Main {
    //思路:
    //
    //发现一个4的倍数可以带走两个位置, X个2可以带走 x-1个位置
    //
    //X 4 X  4 X
    public static void main(String[] args) {
        Scanner sr = new Scanner(System.in);
        int t = sr.nextInt();
        while (sr.hasNext())
        {
            int n = sr.nextInt();
            //int [] an = new int[n];
            int four = 0, two=0;
            for(int i =0;i<n;i++)
            {
                int num = sr.nextInt();
                if(num%4==0)
                    four++;   //能被4整除的,能带走2*four+1个位置
                else if(num%2==0)
                    two++;
            }
            two = Math.max(two-1,0);  //能被2整除的能带走two-1个位置
            if(four*2+1+two>=n)
                System.out.println("Yes");
            else
                System.out.println("No");
            t--;
            if(t==0)
                break;
        }sr.close();
    }
}
发表于 2018-05-29 13:22:47 回复(0)
import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        int count=input.nextInt();//运行次数
        String[] result=new String[count];//结果集
        for(int i=0;i<count;i++){
            int num=input.nextInt();//输入的数的多少
            int[] tmp=new int[num];
            int four_times=0;//四的倍数的数的个数
            int two_times=0;//二的倍数的数的个数
            for(int j=0;j<num;j++){
                tmp[j]=input.nextInt();
                if(tmp[j]%4==0){
                    four_times++;
                }else if(tmp[j]%2==0){
                    two_times++;
                }
            }
            /**
             * 判定条件:
             * 4的倍数可以每隔一个数放  如:1 4 1 4
             * 2的倍数必须挨着放 如:2 2 2 2
             * 因此:在将所有的2的倍数的数去掉
             *         总数:num=num-two_times+1
             */
            if(four_times*2>=num-two_times){
                result[i]="Yes";
            }else{
                result[i]="No";
            }
        }
        for(int i=0;i<count;i++){
            System.out.println(result[i]);
        }
    }
}
发表于 2018-05-27 15:11:59 回复(0)
能整除4的有两种情况:1. A[i]或A[i+1] 本身就能整除4 2.A[i] 和 A[i+1] 同时可以整除2  ,对于第一种情况能整除4的能带走一个不能整除2的,所以记录这三类数字的个数再比较就能出答案了。
public class Main {
 public static void main(String[] args){
     Scanner sc = new Scanner(System.in);
     int t = sc. nextInt();
     while(t-->0){
         int n = sc.nextInt();
         int c =n;
         int e4 = 0,e2=0,e=0; //记录%4 ,%2 和奇数的个数
         while(c-->0){
             int i=sc.nextInt();
             if(i%4==0){
                 e4++;
             }else if(i%2==0){
                 e2++;
             }else e++;
         }
         if(e>e4){
             System.out.println("No");
         }else System.out.println("Yes");
     }
 }       
}
发表于 2020-09-26 00:45:25 回复(0)
import java.util.Scanner;

/**
 * @Classname chongpaishulie
 * @Description 对数列A进行重新排列, 使数列A满足所有的A[i] * A[i + 1](1 ≤ i ≤ N - 1)都是4的倍数。
 * @Date 2020/8/7 14:53
 * @Created by LD
 */
public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while (t-- > 0) {
            int n = sc.nextInt();
            //能被4整除的可以去掉两个位置;x个能被2整除的数可以去掉x-1个位置
            int four = 0, two = 0;
            for (int i = 0; i < n; i++) {
                int num = sc.nextInt();
                if(num % 4 == 0){
                    four++;
                }else if(num % 2 == 0){
                    two++;
                }
            }
            two = Math.max(two-1, 0);
            if(four*2 + two + 1 >= n){
                System.out.println("Yes");
            }else {
                System.out.println("No");
            }
        }
    }

}


编辑于 2020-08-07 15:06:43 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){

 Scanner sr = new Scanner(System.in);
        int t = sr.nextInt();
        while (sr.hasNext())
        {
            int n = sr.nextInt();
            //int [] an = new int[n];
            int four = 0, two=0;
            for(int i =0;i<n;i++)
            {
                int num = sr.nextInt();
                if(num%4==0)
                    four++;   //能被4整除的,能带走2*four+1个位置
                else if(num%2==0)
                    two++;
            }
        //    two = Math.max(two-1,0);  //能被2整除的能带走two-1个位置
           if(two>1){
              if(four*2+two>=n)
                System.out.println("Yes");
            else
                System.out.println("No");
           }
            else if(two==0){
                if(four*2+1>=n)
                System.out.println("Yes");
            else
                System.out.println("No");}
            else {
                   if(four*2+two>=n)
                System.out.println("Yes");
            else
                System.out.println("No");
            }
            t--;
            if(t==0)
                break;
        }sr.close();
        
    }
}
发表于 2018-08-07 16:16:16 回复(0)
import java.util.Scanner;

/**
 * 判断一个正整数列重排列后是不是4的倍数
 *    判断数列满足要求的条件(用捆绑法)
        1.若数列中为4的倍数的元素个数 》=不是4的倍数的元素个数-1则该数列一定满足
        2.当1.不满足时则查找数列中为2的倍数的元素(该元素不是4的倍数),
        此时,把数列中为2的倍数的元素作为一个整体,重排列时把这个整体放在4的倍数后面,
        再将整体的元素个数-1,判断是否满足若数列中为4的倍数的元素个数 》=不是4的倍数的元素个数
 *
 */
public class Main{
    public static void main(String args[]){
        Scanner scn=new Scanner(System.in);
        int row=scn.nextInt();//共有几个数列    
        String rerult[]=new String[row];
        
        for(int i=0;i<row;i++){
          int four=0;
          int two=0;
          int rank=scn.nextInt();//数列中元素的个数
          int src[]=new int[rank];
          for(int j=0;j<rank;j++){
            src[j]=scn.nextInt();
          }        
          for(int j=0;j<rank;j++){
            if(src[j]%4==0){
                four++;
            }
            else if(src[j]%2==0){
                two++;
            }
          }        
            //判断代码
            if(four>=(rank-four-1)||four>=(rank-four-two)){
                rerult[i]="Yes";
            }
           else{
                rerult[i]="No";
            }            
        }
        for(int k=0;k<row;k++){
            System.out.print("  "+rerult[k]);
        }
    }
}
好晕,这样了 为什么报答案错误呀
测试用例:
Bc1:78,c2:781 10 50 948 633 70 638 306 326 158 63 555 426 689 653 863 422 958 594 130 946 839 450 667 957 16 378 768 791 382 678 668 884 353 24 84 574 301 330 617 881 951 424 721 690 674 127 219 356 962 377 780 822 94 460 666 134 892 90 396 866 982 26 98 718 610 512 840 223 320 690 418 534 476 588 81 453 435 917 246 509 9 647 68 464 157 961 441 676 332 640 231 558 402 435 274 277 7 680 358 174 779 357 505 550 590 513 65 359 179 93 60 840 28 703 590 234 888 362 753 684 125 592 467 854 159 781 711 7 42 668 723 569 5 925 427 662 319 267 100 933 230 646 189 427 862 895 934 83 490 782 448 64 230 858 242 998 608 646 908 916 410 864 282 35 688 959 95 450 222 671 741 796 635 589 945 356 233 268 526 775 99 375 473 625 425 418 519 833 414 79 744 148 67 793 924 125 51 576 268 559 109 867 329 438 79 105 972 715 763 221 50 230 92 206 152 286 295 537 705 305 924 368 243 194 123 617 915 225 871 182 701 62 917 801 467 769 252 220 468 935 613 853 94 796 293 591 169 306 805 40 31 746 686 455 190 717 5 869 181 4 291 155 545 814 695 668 570 481 502 257 720 953 518 313 82 302 572 8 748 706 641 409 459 537 404 960 600 242 687 496 828 834 443 53 64 148 924 40 720 756 454 590 130 392 264 544 624 206 879 443 612 655 305 65 365 477 561 414 544 625 875 713 678 298 914 293 170 321 267 641 827 660 387 114 373 132 556 691 490 712 22 897 64 605 857 826 732 685 385 436 29 12 577 105 729 634 777 274 614 20 674 588 472 314 270 916 574 116 936 584 179 572 528 791 318 360 912 790 474 982 34 414 574 100 684 296 666 384 772 246 946 608 786 660 862 228 500 660 304 284 578 890 234 72 261 769 864 770 750 553 7 875 367 183 57 27 260 8 749 469 693 25 225 782 582 528 642 35 572 45 936 622 285 97 539 425 192 261 220 122 227 771 658 79 130 712 612 542 450 323 781 625 932 18 180 504 704 363 550 263 566 24 54 4 514 829 375 251 971 438 655 671 761 401 256 451 789 921 287 199 842 448 307 313 785 556 678 105 201 335 212 608 804 296 742 985 891 638 191 996 485 615 414 327 610 992 406 919 640 593 924 15 674 343 561 248 760 413 908 640 481 187 131 33 202 452 74 802 788 372 436 68 150 556 538 878 356 658 978 838 418 447 776 159 224 112 674 964 223 872 813 341 60 838 255 949 267 
对应输出应该为:
No No No No No Yes Yes No No Yes 
你的输出为:
No No No No No Yes Yes No No Yes
发表于 2018-07-24 15:26:51 回复(0)
4的倍数的个数大于等于奇数的个数就可以了

有bug。。虽然我这么做通过测试了。。

那就换个方式,检测每一个输入的数,如果只有奇数和4的倍数,那么4的倍数的个数大于等于奇数的个数-1.  如果还有第三种数,那么四的倍数的个数大于等于奇数的个数
奇数必须得挨着4的倍数才符合,所以一个奇数必须配备一个4的倍数,(特殊情况为只有奇数和4的倍数,那么4的倍数可以少一个)   如果有第三种数,肯定是非4的倍数的偶数,把他们集体放在最后面就行了,不用管。  

情况1: 1 4 3 4 5 4 2 2 2 2 2
情况2: 1 4 3 4 5 4 7

只有以上两种情况符合题意。  
编辑于 2018-05-29 15:15:57 回复(4)
/*特别笨的方法,希望抛砖引玉
主要想法是找出所有是4的倍数的元素,以及所有不是4的倍数,但是是2的倍数的元素。对他们进行区分处理
*/
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int arrayNum=sc.nextInt();
        for(inti=0;i<arrayNum;i++){
            int ALength=sc.nextInt();
            int fine=0;//是4的倍数的个数
            int twoFine=0;//是2的倍数但不是4的倍数的个数的个数
            int[] array=new int[ALength];
            for(intj=0;j<ALength;j++){
                array[j]=sc.nextInt();
                if(array[j]%4==0)
                    fine++;
                if(array[j]%4!=0&& array[j]%2==0){
                    twoFine++;
                }
            }
            if(twoFine==0){//如果没有不是4的倍数但是是2的倍数的元素,4的倍数的元素大于等于一半就满足条件
                if(fine>=(ALength/2))
                System.out.println("Yes");
            else
                System.out.println("No");
            }else{//4 2 2 5 2 6 ,可以考虑成 2 2 2 6 4 5,将2 2 2 6考虑与4是一起的。
                ALength=ALength-twoFine;
                if(fine>=(ALength-ALength/2))
                    System.out.println("Yes");
                else
                    System.out.println("No");
            }
        }
    }
}
发表于 2018-05-26 16:30:44 回复(0)