Count the number of integers ranging from 1 to n.

Source: Internet
Author: User

Problem:

Given a decimal positive integer N, write down all integers from 1 to n, and then count the numbers of all "1.

For example:
N = 2, write down 1, 2. In this case, only one "1" is displayed ".

N = 12. We will write down 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12. In this way, the number of 1 is 5.

Question 1:

Write a function f (N) and return the number of 1 between 1 and N, for example, F (12) = 5.

Solution 1:

The first method that comes to mind is to traverse 1 ~ N, count the number of each number 1, and then get the number of all 1.

 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 long long int Count(long long int n){ 5     long long int count = 0; 6     while(n){ 7         count += (n % 10 == 1)?1:0; 8         n = n / 10; 9     }10     return count;11 }12 int main()13 {14     long long int n,i,count;15     while(scanf("%lld",&n) != EOF){16         count = 0;17         for(i = 1;i <= n;i++){18             count += Count(i);19         }20         printf("%lld\n",count);21     }22     return 0;23 }

This method is easy to think about, but it is not a good method. The critical problem is efficiency. If the given n is large, it takes a long time to obtain the calculation result.

 

 

Solution 2:

The rule of analysis.

<1> 1-digit status

This is simple. If n = 3, all numbers from 1 to 3: 1, 2, 3. Only one digit appears 1 and only once. It can be found that when n is a single digit, N> = 1, then f (n) = 1; n = 0, F (n) = 0;

<2> 2 digits

<3> three-digit status

 

Likewise, we analyze 4-digit, 5-digit .....

Set n = ABCDE, where ABCDE is the numbers in decimal.

If you want to calculate the number of occurrences of the first hundred bits, it will be affected in three ways: the number of the last hundred bits, the number of the first hundred bits (the lowest bits), and the number of the first hundred bits (the highest bits).

If the number on the hundred bits is 0, the number of occurrences of 1 on the hundred bits may be determined by a higher value. For example: 12013, we can know that the occurrence of 100 bits may be ~ 199,1100 ~ 1199,2100 ~ 2199,..., 11100 ~ 11199. A total of 1200. It can be seen that it is determined by a higher digit (12), and is equal to a higher digit (12) multiplied by the current digit (100 ).

If the number on the hundred bits is 1, the number of times that the hundred bits may appear 1 is not only affected by the higher level, but also by the lower level. For example: 12113, you can know that the impact of hundreds of digits on the high level is: 100 ~ 199,1100 ~ 1199,2100 ~ 2199,..., 11100 ~ 11199. A total of 1200. It is the same as above, and is equal to a higher digit (12) multiplied by the current digit (100 ). But at the same time, it is also affected by the low level, where 12100 ~ 12113, a total of 114, equal to the low number (113) + 1.

If the number on the hundred digits is greater than 1 (2 ~ 9), the first occurrence of a hundred bits is determined only by a higher position. For example, if the value is 12213, the first occurrence of a hundred bits is 100 ~ 199,1100 ~ 1199,2100 ~ 2199,..., 11100 ~ 11199,12100 ~ 12199, a total of 1300, and equal to a higher digit + 1 (12 + 1) multiplied by the current digit (100 ).

1/* n = ABCDE the number on the hundred bits is C 2, so that the number on the hundred bits appears 1. 3 */4 int COUNT = 0; 5 // the number on the hundred digits is 0. The number of occurrences of 1 on the hundred digits is determined by the higher order. 6 if (C = 0) {7 // equal to a higher digit (AB) * Current digit (100) 8 count + = AB * 100; 9} 10 // The number on a hundred digits is 1, the number of times that 1 may appear on a hundred bits is affected not only by a higher position, but also by a lower position. 11 else if (C = 1) {12 // a higher number (AB) * Current digit (100) + low digit (de) + 113 count + = AB * 100 + De + 1; 14} 15 // a hundred digits must be greater than 1 (2 ~ 9), the case where 1 occurs on a hundred bits is determined only by a higher 16 else {17 // (a higher number + 1 (AB + 1) * Current digit (100) 18 count + = (AB + 1) * 100; 19}
1 # include <stdio. h> 2 3 long int count (long int N) {4 // The number of 1 5 long int COUNT = 0; 6 // current digit 7 long int factor = 1; 8 // low digit 9 long int lowernum = 0; 10 // current digit 11 long int currnum = 0; 12 // high digit 13 long int highernum = 0; 14 if (n <= 0) {15 return 0; 16} 17 while (N/factor! = 0) {18 // low digit 19 lowernum = N-(N/factor) * factor; 20 // current digit 21 currnum = (N/factor) % 10; 22 // high digit 23 highernum = N/(factor * 10); 24 // if it is 0, the number of occurrences of 1 is determined by the high digit 25 if (currnum = 0) {26 // equal to the high digit * Current digit 27 count + = highernum * factor; 28} 29 // if it is 1, the number of occurrences of 1 is determined by the upper and lower positions 30 else if (currnum = 1) {31 // high digit * Current digit + low digit + 132 count + = highernum * factor + lowernum + 1; 33} 34 // if the value is greater than 1, the number of occurrences of 1 is determined by the high position 35 else {36 // (high digit + 1 )* Current number of digits 37 count + = (highernum + 1) * factor; 38} 39 // advance one digit 40 factor * = 10; 41} 42 return count; 43} 44 45 int main () {46 long int A; 47 While (scanf ("% LLD", & )! = EOF) {48 printf ("% LLD \ n", count (a); 49} 50 return 0; 51}

Reprinted: http://blog.csdn.net/sjf0115/article/details/8600599

Count the number of integers ranging from 1 to n.

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.