Written test-Squat-java implementation

Source: Internet
Author: User

U.S. Regiment push written programming questions: There are 5 people, number 1,2,3,4,5, referee call to 1, number 1 squat, call to 2, number 2,3 squat, call to 3, number 4,5,1 squat, and so on; First select a person number p, to determine the person in each station squatting, squat output y, not squat output n

Idea: use two int type to record the starting and ending numbers of squat, respectively, m,n

1. When p>=m&&p<=n, explain that P is between m,n

2. After repeated, that is, the station-to-station cumulative more than 5 will appear such as: to 4,5,1 squat situation, at this time p>=m&&p<=5&&p>=0&&p<=n;

The Java code is as follows:

1  Packagetest;2 ImportJava.util.Scanner;3 4 //There are 5 people, number 1,2,3,4,5, referee call to 1, number 1 squat, call to 2, number 2,3 squat, call to 3, number 4,5,1 squat, and so on5  Public classUpanddown {6      Public Static voidDown (intPint[] a) {7         intSum =0;8          for(inti=0;i<a.length;i++){9             intm = (sum+1+5)%5;Ten             //It is possible to have a condition of M 0, but m cannot be 0, at which time m should be 5, so special handling, n similarly One             if(m==0) AM=5; -             intn = (Sum+a[i])%5; -             if(n==0) theN=5; -sum+=A[i]; -             if(m<=N) { -                 if(p>=m&&p<=N) { +System.out.println ("Y" + ""); -}Else{ +System.out.println ("N" + "" "); A                 } at}Else{ -                 if(p>=m&&p<=5&&p>=0&&p<=N) { -System.out.println ("Y" + ""); -}Else{ -System.out.println ("N" + "" "); -                 } in             } -              to         } +     } -  the      Public Static voidMain (string[] args) { *         //TODO auto-generated Method Stub $Scanner in=NewScanner (system.in);Panax Notoginseng         intp=in.nextint (); -        intq=in.nextint (); A         int[] A=New int[Q]; the          for(inti = 0; I < Q && in.hasnext (); i++) { +A[i] =in.nextint (); -         } $ //in.close (); $ Down (p,a); - //in.close (); -     } the  -}

Operation Result:

3 61 2 3 4 4 3

Written test-squat-java implementation

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.