Introduction to algorithm competitions typical example 3-1 light-on problem, 3-1 light-on Problem

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.