NYOJ 77 light on problem

light problem

Time Limit: 3000 ms | Memory Limit: 65535 KB
Difficulty: 1
describe

There are n lights, numbered 1~n, the 1st person turns on all lights, the 2nd person presses all switches numbered in multiples of 2 (these lights will be turned off), and the 3rd person presses all the switches numbered 3 multiples of switches (where lights that are turned off will be turned on, lights that are on will be turned off), and so on. There are k people in total. Which lights are on at the end? Input: n and k, output the number of lights that are on. k≤n≤1000

enter
Enter a set of data: n and k
output
Output the number of lights that are on
sample input
7 3
Sample output
1 5 6 7

#include<iostream>

#include<string.h>
using namespace std;
int main()
{
    int a[1010],n,k,j,i;//j人,i灯
    while(cin>>n>>k)
    {
        //int a[1010]={1};
        memset(a,1,sizeof(a));
        for(j=2;j<=k;j++){
            for(i=1;i<=n;i++)
            {
                if(i%j==0)
                    a[i]=-a[i];
            }
        }
        for(i=1;i<=n;i++)
            if(a[i]>0)
                cout<<i<<" ";
        cout<<endl;
    }
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325896144&siteId=291194637