Turn on the light.
There are n lamps, numbered 1~n. The 1th man turned all the lights on, and the 2nd man pressed the switches of all multiples numbered 2 (the lights would be turned off), and the 3rd man pressed the switch with multiples of 3 (where the lights were turned on, the lights turned off), and so on. A total of k individuals, asked the last which lights on? Enter n and k to output the number of lights that are open. k≤n≤1000.
Sample input:
3 ·
Sample output:
1 5 6 7
My Code is
#include <iostream>using namespacestd;intMainvoid){ intn,k; CIN>>n;//Number of lights ncin>>k;//number of people K inti,j; intA[n]; J=1; for(i=0; i<n;i++) {A[i]=1; } for(j=2; j<=k;j++){ for(i=j-1; i<n;i+=j) {A[i]=-A[i]; } } for(i=0; i<n;i++){ if(a[i]==1) {cout<<i+1<<" "; } } return 0;}
Run results
Turn on the lights--------"Algorithmic Competition introduction Guide" P83