I. Topics and Requirements
- Given a positive integer in decimal, write down all integers starting with 1, to N, and then count the number of "1" that appears.
- Requirement: Write a function f (n) to return the number of "1" occurrences between 1 and N. For example F (12) = 5.
Within a 32-bit integer range, the maximum n of the "F (n) =n" that satisfies the condition is what.
Second, design ideas
Look for the number of occurrences of 1, you can use the enumeration method, statistics 1 The number of occurrences, but the efficiency of this algorithm is not high, then, in a positive integer, 1 appears in the number is regular, if you can find this rule, efficiency should be greatly improved. Example:
|
Bit |
Ten |
Hundred |
。。。 |
3 |
1 |
0 |
0 |
。。。 |
13 |
2 |
4 |
0 |
。。。 |
23 |
3 |
10 |
0 |
。。。 |
33 |
4 |
10 |
0 |
。。。 |
。。。 |
。。。 |
。。。 |
。。。 |
。。。 |
93 |
10 |
10 |
0 (0*100) |
。。。 |
103 |
11 |
10 (1*10) |
4 ((3+1) + (0*100)) |
。。。 |
113 |
12 |
14 (1*10+4) |
14 ((13+1) + (0*100)) |
。。。 |
123 |
12 |
20 ((+) *10) |
24 ((23+1) + (0*100)) |
。。。 |
203 |
21st |
20 (2*10) |
100 ((0+1) *100) |
。。。 |
213 |
22 |
24 (2*10+4) |
100 ((0+1) *100) |
。。。 |
223 |
23 |
30 ((2+1) *10) |
100 ((0+1) *100) |
。。。 |
The above data are analyzed for 10 bits and hundreds respectively, if ABC represents a three-digit number and 10 bits are analyzed, then:
- If b=0, the number of 10 bits is 1 is a*10;
- If b=1, the number of 10 bits is 1 is a*10+c+1;
- If b>1, the number of 10 bits is 1 (a+1) *10;
Promotion: If ABCDE represents an integer of any number of digits, the C-bit is summarized, then:
- If c=0, then the number of C-bit 1 is ab* (c-bit);
- If c=1, then the number of C-bit 1 is ab* (c-bit) +de+1;
- If c>0, the number of C-bit is 1 (ab+1) * (c-bit);
Then, you can find the number of 1 occurrences according to this rule.
Third, the source code
1 //search1.cpp: Defines the entry point of the console application. 2 //3 4#include"stdafx.h"5#include"iostream"6 using namespacestd;7 intMain ()8 {9 intNum//the data to be enteredTen inttag=1;//start counting from the single digit One intCount=0;//Count A intheigher,cur,less;//defines the current bit data cur and the bit heigher higher than him, and less than his lower bit -cout<<"Please enter data:"; -Cin>>num; the while(num/tag!=0) - { -heigher=num/(tag*Ten);//the high position of the current bit -Cur= (Num/tag)%Ten;//Current Bit +Less=num%tag;//the low of the current bit - Switch(cur) + { A Case 0: atcount=count+heigher*tag; - Break; - Case 1: -count=count+heigher*tag+less+1; - Break; - default: incount=count+ (heigher+1)*tag; - Break; to } +tag=tag*Ten; - } thecout<<"1 The number of occurrences is:"<<count<<Endl; * return 0; $}
Iv. Results of the experiment
Five, experimental analysis
Recently reading a book, called "Do you have to light on", is about how to correctly analyze and deal with the problem, like this problem, we can easily think of the way to use enumerations, and the solution is very simple, but this really makes us want it? No, what we need is an efficient and accurate method, although the simplest approach will generally not be wrong, but most of us need to be efficient and correct, then how to do both, it is necessary to explore the law of the problem. But the law of the problem is not so easy to find, so it is necessary to accumulate in peacetime, only a good foundation, to better think about the problem, otherwise, the foundation will not, and how to find a better solution.
Find the number of 1