[Topic] enter an integer n to calculate the number of occurrences of 1 in the n positive numbers from 1 to n. For example, if the input value is 12 and a number is 1, 10, 11, and 12, there are 5 ones in total, the output value is 5.
[Si Lu 1] Almost allAlgorithmThere are intuitive and easy-to-think methods for questions. Of course, the efficiency of such methods is usually not very good, but solving the problem is the first principle. So we should start with the easiest way to think about it. We can traverse every number k from 1 to n. For K, we can calculate the number of 1 contained in it. The method is actually very simple. We only need to determine the single digit, ten digits, and hundreds of digits respectively, if each digit of a thousand bits is 1, then record it with a counter. This idea is very simple, and we can easily write the followingCode:
1 # Include <iostream>
2 # Include < String >
3 Using Namespace STD;
4
5 // Returns the number of 1 contained in any integer k.
6 Int Numbersof1s (unsigned Int K)
7 {
8 Int CNT = 0 ;
9 While (K)
10 {
11 // If this bit is 1, the counter is added with 1;
12 If (K % 10 = 1 )
13 CNT ++;
14
15 K = K/ 10 ;
16 }
17
18 Return CNT;
19 }
20
21 // Returns the number of 1 contained in N integers from 1 to n.
22 Int Numbersof1sfrom1ton (unsigned Int N)
23 {
24 If (N < 0 )
25 Return 0 ;
26
27 Int Count = 0 ;
28 For ( Int I = 1 ; I <= N; I ++)
29 {
30 Count + = numbersof1s (I );
31 }
32
33 Return Count;
34 }
35
36 Int Main ()
37 {
38 Cout <" Please enter the number N: " <Endl;
39 Unsigned Int Number = 0 ;
40 Cin> number;
41
42 Cout < " The numbers of 1 from 1 to n is: " <Endl;
43 Cout <numbersof1sfrom1ton (number) <Endl;
44
45 Return 0 ;
46 }
The running result is as follows:
[Thinking 2] The efficiency of the above algorithms is not very good, especially for N, which takes a long time. In fact, we do not need to calculate the number of 1 contained in each number. We can consider it one by one, the number of all 1 is equal to the number of 1 on a single digit + the number of 1 on ten digits + the number of 1 on a hundred bits + the number of 1 on a thousand bits +... The next question is how to solve the number of 1 on these bits?
First of all, we have the following conclusions that the numbers of 1 in 0-9 are 1, and 10 in 0-99 (10-19) the number of top 1 is 10, and the number of top 1 in the hundreds (999-100) of 0-199 is 100, and so on. Why do we need these numbers? After a simple list, we can easily find that the number of 1 in a single digit is actually related to the number of 10 contained in this number, because for a single digit, it is always a loop from 0-9, the number of top 10 and top 1 are actually related to the number of 100 contained in this number, because every 100 contained has a loop of 0 to 99, and the number of top 10 of 0 to 99 is 10; so what is the relationship? Let's take a look at the following example to understand:
For the number 123:
123/10 = 12, including 12 10, each 10 contains 1 1 (single 1), so the single position contains 12*1 = 12 1; the remainder is discussed separately;
123/100 (or 12/10) = 1, including 1 100, each 100 contains 10 1 (10 1), so 10 contains 1*10 = 10 1; the remainder will be discussed separately later;
123/1000 (or 1/10) = 0, contains 0 1000, each 1000 contains 100 1 (100 1), so contains 0 * = 0 1; the remainder will be discussed separately later;
Consider the two situations of the remainder:
(1) When the remainder is greater than 1:
When Multiple Digits exist, the remainder 3 is greater than 1. Therefore, the number of digits 1 must be + 1;
When there are dozens of digits, the remainder 2 is greater than 1. Because the number between 100-120 (10-19) is increased to 110-119, the number of numbers on the top 10 is greater than 10;
(2) When the remainder is equal to 1:
When there are hundreds of digits, the remainder is equal to 1. We should add one of the 24 numbers, 100-123, and 24 in total;
In the above calculation, we found that 123/1000 = 0 contains 0 1000, So 100 bits contain 0*1. This is a general situation. In fact, because bits are 1, from 100 to n (123) should also be added: 123-100 + 1 1.
To sum up the above situation: For each bit (single, ten, hundred ), we calculate the number of 1 contained in their "normal" (that is, the number of 10,100,100 0 contained in the number multiplied by the corresponding number of 1) + the remainder of the BIT is greater than 1 (equal to 1) in this case, the number of contained 1 = the total number of 1 on this bit, all the bitwise traversal, sum, and OK. Based on this idea, we can get the following code:
1 # Include <iostream>
2 # Include < String >
3 Using Namespace STD;
4
5 Int Numbersof1sfrom1ton (unsigned Int N)
6 {
7 Int Currentn = N;
8
9 // Counter
10 Int CNT = 0 ;
11
12 // Calculates the number of 10,100,100 0 s contained in the number.
13 Int Quotient = 0 ;
14
15 // Remainder: calculates the number of 1 contained in the remaining number except the integer.
16 Int Remainder = 0 ;
17
18 // The weights in each cycle record the numbers of 1, 10, and 10,100,100 bits in 0 respectively;
19 Int Mult = 1 ;
20
21 While (Currentn)
22 {
23 Quotient = currentn/ 10 ;
24 Remainder = currentn % 10 ;
25
26 // How many 10,100,100 zeros are contained, multiplied by the corresponding number of single-digit 1, 10-digit 1, and-digit 1
27 CNT + = quotient * mult;
28
29 // If the remainder is greater than 1, add a weight for this round.
30 If (Remainder> 1 )
31 {
32 CNT + = mult;
33 }
34 // The remainder is equal to 1.
35 Else If (Remainder = 1 )
36 {
37 CNT + = N-currentn * mult + 1 ;
38 }
39
40
41 Currentn = currentn/ 10 ;
42 Mult * = 10 ;
43 }
44
45 Return CNT;
46 }
47
48 Int Main ()
49 {
50 Cout < " Please enter the number N: " <Endl;
51 Unsigned Int Number = 0 ;
52 Cin> number;
53
54 Cout < " The number of 1 s from 1 to n is: " <Endl;
55 Cout <numbersof1sfrom1ton (number) <Endl;
56
57 Return 0 ;
58 }
The running result is as follows:
References:
ProgramMember interview questions featured 100 questions: http://zhedahht.blog.163.com/blog/static/25411174200732494452636/
Note:
1) Compile all the code environments in this blogWin7 + vc6. All code has been debugged by the blogger.
2) BloggerPython27This blogArticleCopyright. For Network reprinting, please indicate the sourceHttp://www.cnblogs.com/python27/. You have any suggestions for solving the problem. Please feel free to comment on them.