Light on problem time limit: 3000 MS | memory limit: 65535 kb difficulty: 1
-
Description
-
There are n lamps numbered 1 ~ N, 1st people turn on all the lights, 2nd people press the switch in multiples of 2 (these lights will be turned off ), 3rd people press all the switches (the switched lights will be turned on, the lights will be turned off) numbered 3, and so forth. A total of K people asked which lights were on at last? Input: N and K, and output the number of lights on. K ≤ n ≤ 1000
-
Input
-
Enter a set of data: N and K
-
Output
-
Number of the lights on
-
Sample Input
-
7 3
-
Sample output
-
1 5 6 7
Code:
#include<stdio.h>int main(){int n,k,i,j,f[1001];scanf("%d%d",&n,&k);for(i=1;i<=n;++i)f[i]=1;for(i=2;i<=k;++i){for(j=i;j<=n;j+=i)f[j]=f[j]*-1;} printf("1"); for(i=2;i<=n;++i) if(f[i]==1) printf(" %d",i);printf("\n");return 0;}
Nyoj-light on Problem