Count 1 between 0 and N

Source: Internet
Author: User

Problem Description

Given a decimal integer n, the number of "1" occurrences in all integers from 1 to n is calculated.  

For example: 1 "1" appeared in n=2.

n=12 when 1,2,3,4,5,6,7,8,9,10,11,12. There were 5 "1".

Method one brute force solution

The most direct way is to start from 1 to N, each of which contains the number of "1" added together, the problem is solved.

The code is as follows:

publicclassCountOnes {
    publiclong CountOnes(longn){
        longi = 0,j = 1;
        longcount = 0;
        for(i = 1; i <= n; i++)
        {
            j = i;
            while(j != 0)
            {
                if(j % 10== 1)
                    count++;
                j = j / 10;
            }
        }
        returncount;
    }
    publicstatic voidmain(String args[]){
        CountOnes c = newCountOnes();
        System.out.println(c.CountOnes(12));
        System.out.println(c.CountOnes(120));
    }
}

The time complexity of the algorithm is O (N*LGN)

Solution Two

1-digit situation:

In the solution two has been analyzed, greater than or equal to 1, there are 1, less than 1 is not.

2-digit situation:

N=13, the number of single-digit occurrences of 1 is 2, respectively 1 and 11, 10 digits 1, respectively, is 10,11,12,13, so f (N) = 2+4.

N=23, the number of single-digit occurrences of 1 is 3, respectively, 1,11,21, 10 digits 1 of the number of 10, respectively, 10~19,f (N) =3+10.

Thus we found that the number of single-digit occurrences of 1 is not only related to single digit, and 10 digits are also related, if single digit is greater than or equal to 1, the number of single digit 1 is 10 digits plus 1, if single digit is 0, the number of single digit 1 is equal to 10 digit number. The number of 10-digit occurrences of 1 is not only related to the 10-digit number, but also to the single-digit: If the 10-digit number equals 1, 10 is the number of digits on the 1-digit number plus 1, and if the 10-digit is greater than 1, 10 is 1 on the 10-digit number.

3-digit situation:

N=123

The number of single digit occurrences is 13:1,11,21,...,91,101,111,121.1

The number of 10-bit occurrences of 1 is 20:10~19,110~119

The number of hundreds of 1 appears to be 24:100~123

We can continue to analyze 4-digit, 5-digit numbers and derive the following general situation:

Suppose N, we are going to calculate the number of occurrences of 1 on the hundred, will be determined by three parts: the number on the hundred, the number above the hundred, the number of the hundred.

If the number on the hundred is 0, then the number of 1 on the hundred is determined only by a higher level, such as 12013, 1 of the case is 100~199,1100~1199,2100~2199,...,11100~11199, a total of 1200. equals the higher number multiplied by the current number of digits, which is 12 * 100.

If the number on the hundred is greater than 1, then the number of occurrences of 1 on the hundred is determined only by higher levels, such as 12213, 1 of the total is 1300 100~199,1100~1199,2100~2199,...,11100~11199,12100~12199. equals the higher number plus 1 times the current number of digits, i.e. (12 + 1) *100.

If the number on the hundred is 1, the number of 1 on the hundred will be affected not only by higher levels but also by low levels. For example 12113, affected by high Level 1 of the situation: 100~199,1100~1199,2100~2199,...,11100~11199, a total of 1200, but it is also affected by the low, 1 of the situation is 12100~12113, a total of 114, equals Low digit 113+1.

To synthesize the above analysis, write the following code:

01 publiclong CountOne2(longn) {
02         longcount = 0;
03         longi = 1;
04         longcurrent = 0, after = 0, before = 0;
05         while((n / i) != 0) {
06             current = (n / i) % 10;
07             before = n / (i * 10);
08             after = n - (n / i) * i;
09
10             if(current > 1)
11                 count = count + (before + 1) * i;
12             elseif(current == 0)
13                 count = count + before * i;
14             elseif(current == 1)
15                 count = count + before * i + after + 1;
16
17             i = i * 10;
18         }
19         returncount;
20 }

Count 1 between 0 and 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.