Huawei trial (7)

Source: Internet
Author: User

Intermediate question: 100 points
Description: an N (1 ≤ n ≤ 65535) electric light is installed in the corridor, 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: number of electric lights
Output: Number of lights on
Sample input: 3, sample output: 1


Solution 1: If you want to simulate this question, it is certainly possible, but it is hard for students with poor code Foundation to write it. Here, we will write a simulation method.
Solution 2: think more about this question. We can see that for any lamp, such as 12, the factors include 1, 2, 3, 4, 6, and 12. Note: Students numbered 1, 2, 3, 4, 6, and 12 need to pull it. Here we find that 1*12 = 12, 2*6 = 12, 3*4 = 12, so the factors are all one-to-one, that is, the 1 pull is offset by 12, and the 2 pull is offset by 6. So who will not be offset? The answer is the number of percentages. For example, if the factor of 9 is or 9, then 3*3 = 9, and 3 is pulled only once, it will not be offset. Therefore, the answer to this question is that the number of records within the input N is equal to (INT) SQRT (n ).
From this question, we can see that in many programming cases, the thinking test is much more useful than the hyperactivity pen.

#include<iostream>using namespace std;void main(){    int n;    cin>>n;    int numLightOn = 0;    for(int i=1;(i*i)<=n;i++)       numLightOn++;    cout<<numLightOn<<endl;  }

The simulated code is as follows:

# Include <iostream> # include <vector> using namespace STD; void main () {int N; CIN> N; vector <int> light (n + 1, 0 ); // 0 indicates extinct, and 1 indicates bright int numlighton = 0; For (INT I = 1; I <= N; I ++) // n people {Int J = I; int times = 2; while (j <= N) {Light [J] = 1-light [J]; // 0-> 1, 1-> 0 J = I; // The J lamp J * = times; times ++ ;}} // end for (INT I = 1; I <= N; I ++) {If (light [I] = 1) numlighton ++ ;} cout <numlighton <Endl ;}

 

 

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.