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