C language to realize the problem of turn on the light

Source: Internet
Author: User

Turn on the light question:

There are n lights, numbered 1~n, the 1th person turns all the lights on, the 2nd person presses all the switches with multiples of 2 (these lights will be turned off), and the 3rd person presses the switch with multiples of 3 (where the lights will be turned on, the lights on will be 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.

Attached code:

#include <stdio.h>#include<string.h>#defineMAX 1000+10intA[max]; intMain () {intI, J, n=7, k=3; //memset (A, 0, sizeof (a));          for(i =1; I <= K; i++) {//number             for(j =1; J <= N; J + +) {//Number of Lights                if(j%i = =0) A[j] =!A[j]; //Press the switch number for their number multiples, define the switch status of the lamp by 0,1            }        }         for(i =1; I <= N; i++) {//number of traversing lights        if(A[i])//If the lights are on, run        {              if(i = = N) {//The last lampprintf"%d,", i); }            Elseprintf"%d", i); }        }        return 0; }  

C language to realize the problem of turn on the light

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.