[ACM] lights problem

Time Limit: 3 Sec Memory Limit: 64 MB
Submit: 277 Solved: 176

Title Description

N-lamp, numbered 1 ~ n, all the individual lamps of the first open, all the second individual pressing switches numbered multiple of 2 (the lamps are turned off), the third press all individuals numbered 3 multiple switch (which will be opened to turn off the lamp, driving the lamp is turned off), and so on. A total of k individual, and finally asked what the lights are on? Input: n and k, the output of the lamp driving number. k≤n≤1000

Entry

A set of input data: n and k

Export

Open numbered light output

Sample input

7 3

Sample Output

1 5 6 7
#include<stdio.h>
int main(){
    int n,k;
    static int data[1000];
    scanf("%d %d",&n,&k);
    for(int i = 1;i <= n;i++){
    	for(int j = 1;j <= k; j++){
            if(i % j == 0){
                if(data[i] == 0)
                    data[i] = 1;
                else data[i] = 0;
            }
        }
    }
    for(int i = 1; i <= n; i++)
        if(data[i] == 1)
        printf("%d ",i);
}
Published 46 original articles · won praise 39 · views 40000 +

Guess you like

Origin blog.csdn.net/weixin_42128813/article/details/103773958