Bulb programming Problem-java

Source: Internet
Author: User

Now there are 100 bulbs, each light bulb is closed, the bulb is sorted to 1~100, then the light bulb switch of 2 multiples is pressed, then the lamp switch of 3 multiples is pressed ... Until you click on the lamp switch of multiples of N, count the number of light bulbs.

Algorithm ideas:

1. Storing the number of bulbs in an array

2. Array default value is 0

3. Change the light bulb of the multiple of n from the 2~n value, if the bulb itself is 0, then assign a value of 1, otherwise the assignment is 0.

classlight{ Public Static voidMain (string[] args) {int[] Nums =New int[100]; intindex = 0; intN = 2;  for(inti=0;i<100;i++) {Nums[i]= 0; }         for(inti=2;i<=n;i++){             for(intj=1;j<100;j++) {Index= J+1; if(index>=i&&index%i==0){                    if(nums[j]==0) {Nums[j]=1;} Else{nums[i]=0;} }            }        }        intCount = 0;  for(inti=0;i<100;i++){            if(nums[i]==1) {count++;} } System.out.println ("Result=" +count); }}

Bulb programming Problem-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.