NetEase 2018 School Recruit Pen Test-array can not meet the reordering of any adjacent element product is a multiple of 4

Source: Internet
Author: User

Today, I looked at NetEase's latest school recruit written questions:

Small easy to have a positive integer sequence of length N = {A[1], a[2], a[3] ..., A[n]}. Dr. Niu gave a little difficulty:
The logarithmic column A is rearranged so that the sequence a satisfies all A[i] * a[i + 1] (1≤i≤n-1) is a multiple of 4.
Small easy now need to determine whether a sequence can be re-ordered to meet the requirements of Dr. Niu.

The code is as follows:

1 ImportJava.util.Scanner;2 3 /**4 * Created by JY on 2017/9/9.5  */6  Public classFourbeishu {7      Public Static BooleanIsRequired (intLengthint[] a) {8         intJishu = 0;9         intFour = 0;Ten         intOushu = 0; One          for(inti = 0; I <= length-1; i++) { A             //does not meet the requirements -             if(A[i] <= 0) { -                 return false; the             } -             //odd number of numbers -             if(A[i]% 2 = = 1) { -jishu++; +             } -             //just two times the number of numbers +             if(A[i]% 2 = = 0 && a[i]% 4! = 0) { Aoushu++; at             } -             //multiples of 4 -             if(A[i]% 4 = = 0) { -four++; -  -             } in         } -         //A multiple of 4 is greater than half-1 of the array's length, which can satisfy a[i] and a[i+1] must have a multiple of 4, thus missing all even-numbered cases, such as 2 2 6 to         if(Four >= length-four-1) { +             return true; -         } the         //The number of multiples of 4 must be greater than or equal to odd number (if there are odd words), at the same time, an even number must be at least 2, 1 3 5 4 4 4 2 2 2 2 2 2 2 2 2 *         if(Four >= Jishu) && (Oushu >= 2)) { $             return true;Panax Notoginseng         } -  the         return false; +     } A  the      Public Static voidMain (string[] args) { +Scanner in =NewScanner (system.in); -         intnum =in.nextint (); $         intN; $         int[] A; -          for(inti = 0; i < num; i++) { -n =in.nextint (); theA =New int[n]; -              for(intj = 0; J < N; J + +) {WuyiA[J] =in.nextint (); the             } -             Booleanb =isrequired (n, a); Wu             if(b) { -System.out.println ("Yes"); About}Else { $System.out.println ("No"); -             } -         } -     } A}

NetEase 2018 School Recruit Pen Test-array can not meet the reordering of any adjacent element product is a multiple of 4

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.