C language daily practice (2) -- Turn on the light

Source: Internet
Author: User

For example, if the light is turned on

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 will be turned on, the switched light will be turned off), and so on. A total of k people asked which lights were on at last? Input: n and k, and output the numbers of open and so on. K <= n <= 1000.

Example input: 7 3

Sample output: 1 5 6 7

Solution: open an array that is large enough to simulate the entire process ~

Code:

# Include

 
  
# Include

  
   
# Define MAX 1000 + 10int a [MAX]; int main () {int I, j, n, k; memset (a, 0, sizeof ()); scanf ("% d", & n, & k); for (I = 1; I <= k; I ++) for (j = 1; j <= n; j ++) if (j % I = 0) a [j] =! A [j]; for (I = 1; I <= n; I ++) if (a [I]) {if (I = n) printf ("% d \ n", I); else printf ("% d", I);} return 0 ;}

  

 

Related Article

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.