Transferred from: http://blog.csdn.net/sjf0115/article/details/8600599
Problem:
Given a decimal positive integer n, write down all integers starting with 1, to N, and then count the number of all "1" that appear.
For example:
N= 2, write down 1, 2. There are only 1 "1" of these.
N= 12, we will write down 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12. Thus, the number of 1 is 5.
Question one:
Write a function f (n), which returns the number of 1 occurrences between 1 and N, such as f (12) = 5.
Solution One:
Let us first think of one method is: Traverse 1~n, Count 1 each number of occurrences, add to get all the number of 1.
[CPP]View Plaincopy
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- A long long int Count (long long int n) {
- A long long int count = 0;
- While (n) {
- Count + = (n% 10 = = 1)? 1:0;
- n = N/10;
- }
- return count;
- }
- int main ()
- {
- long long int n,i,count;
- while (scanf ("%lld", &n)! = EOF) {
- Count = 0;
- For (i = 1;i <= n;i++) {
- Count + = count (i);
- }
- printf ("%lld\n", count);
- }
- return 0;
- }
Although this method is easy to think about, it is not a good method. The fatal problem is the efficiency problem. If the given n is large, it takes a long time for the results to be calculated.
Solution Two:
The regularity of the analysis.
<1>1 Number of digits
This simple, if n = 3, then all the numbers from 1 to 3: The single digit appears 1, and only occurs once. It can be found that n is single digit, n >=1, then f (n) = 1;n = 0,f (n) = 0;
<2>2 Number of digits
<3>3 Number of digits
Analysis of 4-digit, 5-digit number ...
Set n = ABCDE, where abcde are the numbers on each of the decimal members.
If you want to calculate the number of occurrences of 1 in the hundreds, it will be affected by 3 aspects: the number on the hundred, the number on the Hundred (low), the number on the Hundred (high).
If the number on the hundred is 0, the hundred may appear 1 times higher. For example: 12013, you can know that the hundred 1 of the situation may be: 100~199,1100~1199,2100~2199,,.........,11100~11199, altogether 1200. It can be seen that the higher number (12) is determined and is equal to the higher number (12) multiplied by the current number of digits (100).
If the number on the hundred is 1, the number of possible 1 on the hundred will be affected not only by higher levels but also by low levels. For example: 12113, you can know that the hundred affected by the high position is: 100~199,1100~1199,2100~2199,,.........,11100~11199, altogether 1200. The same as above, and equals the higher number (12) multiplied by the current number of digits (100). But at the same time it is also affected by low, hundreds of 1 of the situation is: 12100~12113, a total of 114, equal to the low number (113) +1.
If the number on the hundred is greater than 1 (2~9), then 1 of the situation on the hundred is determined by a higher position, such as 12213, then 1 of the Hundred occurrences are: 100~199,1100~1199,2100~2199,...........,11100~11199,12100 ~12199, a total of 1300, and equals a higher number +1 (12+1) multiplied by the current number of digits (100).
[CPP]View Plaincopy
- /*n = ABCDE Hundred on the number is C
- Just for the sake of a 1 of the hundreds of cases.
- */
- int count = 0;
- The number on the hundred is 0, and the hundred may appear 1 times higher.
- if (c = = 0) {
- //equals higher digit (AB) * Current number of digits (+)
- Count + = ab*100;
- }
- Hundreds on the number of 1, the Hundred may appear 1 times not only by the higher influence also affected by the low
- else if (c = = 1) {
- //higher digit (AB) * Current digits (100) + low number (DE) +1
- Count + = ab*100 + de + 1;
- }
- The number on the hundred is greater than 1 (2~9), and a 1 on the hundred is only determined by a higher position.
- else{
- //(higher number +1 (ab+1)) * Current number of digits (+)
- Count + = (AB + 1) * 100;
- }
[CPP]View Plaincopy
- #include <stdio.h>
- A long long int Count (long long int n) {
- Number of//1
- A long long int count = 0;
- //Current bit
- long long int Factor = 1;
- //Low digit
- long long int lowernum = 0;
- //Current bit number
- long long int currnum = 0;
- //High -level digital
- long long int highernum = 0;
- if (n <= 0) {
- return 0;
- }
- While (n/factor! = 0) {
- //Low digit
- Lowernum = N-(n/factor) * Factor;
- //Current bit number
- Currnum = (n/factor)% 10;
- //High -level digital
- Highernum = N/(Factor * 10);
- //If 0, the number of occurrences 1 is determined by the high
- if (Currnum = = 0) {
- //equals high number * Current number of digits
- Count + = Highernum * Factor;
- }
- //If 1, the number of occurrences 1 is determined by high and low
- Else if (currnum = = 1) {
- //high number * Current digits + low number + 1
- Count + = Highernum * Factor + lowernum + 1;
- }
- //If greater than 1, the number of occurrences 1 is determined by the high
- else{
- //(high digit + 1) * Current number of digits
- Count + = (highernum + 1) * Factor;
- }
- //move forward one
- Factor *= 10;
- }
- return count;
- }
- int main () {
- long long int A;
- while (scanf ("%lld", &a)! = EOF) {
- printf ("%lld\n", Count (a));
- }
- return 0;
- }
Count of 1 occurrences in a positive integer from 1 to n