Huawei camera test-Number of lights on

Source: Internet
Author: User

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

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.