Number of lights on
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.
Implemented in C ++:
1 #include <iostream> 2 using namespace std; 3 4 int main() 5 { 6 int n; 7 bool light[100]; 8 int count; 9 while(cin>>n)10 {11 count = 0;12 int i,j;13 for (i=1;i<=n;i++)14 {15 light[i]=false;16 }17 for (i=1;i<=n;i++)18 {19 for (j=1;j<=i;j++)20 {21 if (i%j==0)22 {23 light[i]=!light[i];24 }25 }26 }27 for (i=1;i<=n;i++)28 {29 if (light[i])30 {31 count++;32 }33 }34 cout<<count<<endl;35 }36 return 0;37 }
Running result:
Huawei camera test-Number of lights on