Algorithm problem-hundred lights to extinguish

Source: Internet
Author: User

Title: There are 100 lamps, numbered 1~100, respectively corresponding to 100 switch control.   Start all facing up (means open, facing down), and then do the following: the reverse direction of a multiple of number 1, the reverse direction of the number of multiples of 2, and the reverse direction of a multiple of 3 is numbered. A switch is dialed in the opposite direction with the number 100, and the number of the lamp that was last turned off.

Here's My Code

BOOL[] Light =New BOOL[ -];  for(inti =0; I < -; i++) Light[i]=true;//all lights are in the initial state of open             for(inti =0; I < -; i++)                 for(intj =0; J < -; J + +)                    if(j +1)% (i +1) ==0)//If the number is a multiple of nLIGHT[J] = light[j] = =false?true:false;//Reverse Dial Switch             for(inti =0; I < -; i++)                if(Light[i] = =false) Console.WriteLine ("Section"+ (i +1) +"number lights are off");

Here's the heartbreaking code of a great God.

 for (int1; I <= sqrt (   "First" " lights are off " << Endl;

Study it!

Algorithm problem-hundred lights to extinguish

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.