紫书39页
把 i 的倍数改变它的真假 直到超过范围 多次遍历
用 bool 的数组代表灯 灯亮 = true 灯灭 = false
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <cstring> 5 6 using namespace std; 7 8 int main() 9 { 10 bool a[1005] = {false}; 11 int i, k, n, j=0; 12 scanf("%d%d",&n,&k); 13 for(i=1;i<=k;i++) 14 { 15 j = i; 16 while(j<=n) 17 { 18 if(a[j]==false) 19 a[j] = true; 20 else 21 a[j] = false; 22 j = j + i; 23 } 24 } 25 printf("1"); 26 for(i=2;i<=n;i++) 27 { 28 if(a[i] == true) 29 printf(" %d",i); 30 } 31 printf("\n"); 32 return 0; 33 }
原文:http://www.cnblogs.com/16-CHQ/p/6427936.html