算法竞赛入门经典第二版39
#include <iostream> const int maxn = 1000 ; int light[maxn]={1}; using namespace std ; int main() { int n ,k ; cin >> n >> k ; for ( int i = 1 ;i <= k ;++i) for ( int j = 1 ; j <= n ; ++j) if (j%i ==0 ) light[j] = !light[j]; for ( int i = 1 ; i <=n ; ++i) if (light[i] == 1) cout << i << " " ; cout << endl ; return 0 ; }
这里假设灯开亮的时候为1 ,灭的时候为0 。