Introduction to algorithm competitions typical example 3-1 light-on problem, 3-1 light-on Problem
There are n lamps numbered 1 ~ N, 1st people turn on all the lights, 2nd people press the switch in multiples of 2 (these lights will be turned off ), 3rd people press the switch in multiples of 3 (the switched light is turned on, and the light is turned off), and so on. A total of k people asked which lights were on at last?
Input: n and k, and output the number of lights on. K ≤ n ≤1000.
Example input: 7 3
Sample output: 1 5 6 7
# Include <stdio. h> # include <stdlib. h> # include <string. h> # define MAXN 1000 + 10int a [MAXN]; int main (int argc, char * argv []) {int I, j, n, k, first = 1; scanf ("% d", & n, & k); memset (a,-1, sizeof (a); for (I = 1; I <= 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); // there should be no space at the end */for (j = 1; j <= n; j ++) if (a [j] = 1) {if (first) first = 0; else printf (""); printf ("% d", j );} printf ("\ n"); // Finally, output the linefeed system ("PAUSE"); return 0 ;}
Conclusion: 1. Use a flag to determine whether the output is the first time. If yes, no space is output. Otherwise, enter a space and a number.
2. The switch lamp can also be set to 0 1.
Algorithm Competitions
"The largest Poyang Lake, the smallest Taihu Lake, the second Hongze Lake, and the third Lake lake"
A classic question about Liu rujia's algorithm Competition
The landlord will not compile the entire code method compiler. The C language does not have this syntax. It should be a clerical error or pseudocode. The landlord can infer it based on the entire program.