Introduction:
A corridor contains N (1 ≤ n ≤ 65535) lamps, numbers 1, 2, 3 ,... N-1 and N. Each lamp is controlled by a wire pulling switch. Start, all the lights are turned off.
N students passed through the corridor. The first student pulls the switch of the electric lamp whose number is a multiple of 1, and the second student pulls the switch of the electric lamp whose number is a multiple of 2; then the third student pulls the switch of the electric lamp whose number is a multiple of 3. Then, the nth student pulls the switch of the electric lamp whose number is a multiple of N. After N students finished according to this rule, there were several lights in the corridor.
Note: The number of electric lights is the same as that of students.
Input: 3
Output: 1
I tried it twice for the first time and read it on the Internet. I think I can directly assign values by multiples, which improves the speed a lot.
1 # include <stdio. h> 2 int A [65536]; 3 int light (int n) 4 {5 Int I, j, sum = 0; 6 for (I = 1; I <= N; I ++) 7 for (j = 1; j * I <= N; j ++) 8 A [J * I] =! A [J * I]; 9 for (I = 1; I <n; I ++) 10 if (a [I]) 11 sum ++; 12 Return sum; 13} 14 15 int main () 16 {17 int N; 18 scanf ("% d", & N); 19 printf ("% d \ n ", light (n); 20}
Number of light bulbs