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