Count the number of occurrences of 1 in a positive integer from 1 to n

Source: Internet
Author: User

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
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. A long long int Count (long long int n) {
  5. A 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. }

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
  1. /*n = ABCDE Hundred on the number is C
  2. Just for the sake of a 1 of the hundreds of cases.
  3. */
  4. int count = 0;
  5. The number on the hundred is 0, and the hundred may appear 1 times higher.
  6. if (c = = 0) {
  7. //equals higher digit (AB) * Current number of digits (+)
  8. Count + = ab*100;
  9. }
  10. Hundreds on the number of 1, the Hundred may appear 1 times not only by the higher influence also affected by the low
  11. else if (c = = 1) {
  12. //higher digit (AB) * Current digits (100) + low number (DE) +1
  13. Count + = ab*100 + de + 1;
  14. }
  15. The number on the hundred is greater than 1 (2~9), and a 1 on the hundred is only determined by a higher position.
  16. else{
  17. //(higher number +1 (ab+1)) * Current number of digits (+)
  18. Count + = (AB + 1) * 100;
  19. }

[CPP]View Plaincopy
    1. #include <stdio.h>
    2. A long long int Count (long long int n) {
    3. Number of//1
    4. A long long int count = 0;
    5. //Current bit
    6. long long int Factor = 1;
    7. //Low digit
    8. long long int lowernum = 0;
    9. //Current bit number
    10. long long int currnum = 0;
    11. //High -level digital
    12. long long int highernum = 0;
    13. if (n <= 0) {
    14. return 0;
    15. }
    16. While (n/factor! = 0) {
    17. //Low digit
    18. Lowernum = N-(n/factor) * Factor;
    19. //Current bit number
    20. Currnum = (n/factor)% 10;
    21. //High -level digital
    22. Highernum = N/(Factor * 10);
    23. //If 0, the number of occurrences 1 is determined by the high
    24. if (Currnum = = 0) {
    25. //equals high number * Current number of digits
    26. Count + = Highernum * Factor;
    27. }
    28. //If 1, the number of occurrences 1 is determined by high and low
    29. Else if (currnum = = 1) {
    30. //high number * Current digits + low number + 1
    31. Count + = Highernum * Factor + lowernum + 1;
    32. }
    33. //If greater than 1, the number of occurrences 1 is determined by the high
    34. else{
    35. //(high digit + 1) * Current number of digits
    36. Count + = (highernum + 1) * Factor;
    37. }
    38. //move forward one
    39. Factor *= 10;
    40. }
    41. return count;
    42. }
    43. int main () {
    44. long long int A;
    45. while (scanf ("%lld", &a)! = EOF) {
    46. printf ("%lld\n", Count (a));
    47. }
    48. return 0;
    49. }

Count of 1 occurrences in a positive integer 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.