Huawei on-Machine test (number of lights-java)

Source: Internet
Author: User

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)

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.