PS: Oneself write, self test OK, for everybody reference.
/*
A corridor is followed by N (1≤n≤65535) lamps, from beginning to end numbered 1, 2, 3 、... n-1, N. Each lamp is controlled by a pull-wire switch. At first, the lights were all closed.
There were n students walking through the corridor. The first student pulls the switch of the electric light with a number of multiples of 1, then the second student pulls the switch of the electric light with a number of multiples of 2, then the third student pulls the switch of the electric light with a number of multiples of 3;
So continuing, the last nth student pulls the switch of the light bulb, which is usually a multiple of n. N students according to the provisions of the walk, there are several lights in the corridor.
Note: The number of lights is the same as the number of students.
*/
1 ImportJava.util.Scanner;2 3 Public classLamp {4 5 Public Static voidMain (string[] args) {6System.out.println ("Please enter the number of lights in the corridor:");7Scanner s =NewScanner (system.in);8 intn =s.nextint ();9 s.close ();Ten One if((n < 1) | | (N > 65535)) A { -SYSTEM.OUT.PRINTLN ("Input value out of range! "); - } the Else - { -System.out.println ("The Corridor has" +getlampnum (n) + "light on"); - } + } - + Private Static intGetlampnum (intN) { A at intRET = 0; - Boolean[] flag =New Boolean[n]; - //Initialize the lights, all of them off. - for(inti = 0; I < n; i++) - { -Flag[i] =false; in } - to //Pull light operation, true to light, off to false + for(inti = 1; I <= N; i++) - { the for(intj = i; J <= N; J + +) * { $ if(0 = = j%i)Panax Notoginseng { -FLAG[J-1] =!flag[j-1]; the } + } A } the + //Find all the bright lights - for(inti = 0; I < n; i++) $ { $ if(Flag[i]) - { -ret++; the } - }Wuyi the returnret; - } Wu -}
Huawei on-Machine test (number of lights-java)