Number of light bulbs

Source: Internet
Author: User

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

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.