Page [p39] lights problem

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/u011544909/article/details/80050154

Problems lights, n-lamp, numbered 1 ~ n. The first person to all the lights are turned on, all the second individual pressing switches numbered multiple of 2 (the lamps are turned off), all the third number is a multiple of pressing the switch 3 (wherein switching off the lamp is turned on, driving the lamp will be turned off), and so on. A total of k individual, and finally asked what the lights are on?

[Sample input]
73

Sample Output] [
1567


Analysis: an array to whether that number is 1, 2 ... lamp driving ..n, these operations can be simulated.
The value of each array is initialized to 0, then a lamp is represented by the binary state 0 is off, 1 on, each corresponding to a number of state lamp 1 becomes 0, the state of the lamp becomes 0 1 can be.

#include <bits/stdc++.h>
#define MAXN 1010
using namespace std;
static int a[MAXN];
int main()
{
    int n,k,first=1;
    while(cin>>n>>k)
    {
        memset(a,0,sizeof a);
        for(int i=1;i<=k;i++)
            for(int j=1;j<=n;j++)
            {
                if(j%i==0)a[j]=!a[j];
            }
        for(int i=1;i<=n;i++)
        if(a[i]) {if(first) first=0;else cout<<" ";cout<<i;} //防止第一个数前输出空格
        cout<<endl;
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/u011544909/article/details/80050154