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