There are n lights, numbered 1~n, the 1th person to turn all the lights on, the 2nd person presses all the switches numbered in multiples of 2 (these lights will be turned off), the 3rd person presses all the switches numbered in multiples of 3 (where the lights are turned on, the lights are turned off), and so on. A total of k individuals, asked the last which lights on?
Input: N and K, the output light number of the open. k≤n≤1000.
Sample Input: 7 3
Sample output: 1 5 6 7
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXN 10int A[maxn];int Main ( int argc, char *argv[]) { int i, J, N, K, first = 1; scanf ("%d%d", &n, &k); Memset (A,-1, sizeof (a)); for (i = 1; l <= K; i++) for (j = 1; J <= N; j + +) { if (j%i = = 0) A[j] =-a[j]; } /* for (j = 1; J <= N; j + +) if (a[j] = = 1) printf ("%d", j); Finally there should be no space */For (j = 1; J <= N; + j) if (a[j] = 1) {if (first) first = 0; else printf (""); printf ("%d", j);} printf ("\ n"); Finally, the output line break system ("PAUSE"); return 0;}
Summary: 1 Use a flag to determine whether the first output, if, do not output space, or lose the first space after the loss of digital
2 switching lights can also be used 0 1
Algorithm Contest Introduction Classic example 3-1 open light problem