The number of occurrences of 1 in an integer ranging from 1 to n of offer (32)

Source: Internet
Author: User
Tags ranges

Question: enter an integer to calculate the number of occurrences of 1 in the decimal representation of the N integers from 1 to n.

Method 1: The most intuitive solution T (n) = O (nlgn)

int NumberOf1Between1AndN_Solution1(unsigned int n){    int number = 0;    for(unsigned int i = 1; i <= n; ++ i)        number += NumberOf1(i);    return number;}int NumberOf1(unsigned int n){    int number = 0;    while(n)    {        if(n % 10 == 1)            number ++;        n = n / 10;    }    return number;}

 

Method 2:

Assume n = ABCDE (in decimal notation of N, E is a single digit)

If you want to calculate the number of times that a hundred bits of C appears 1, it will be affected by three factors:

(1) Hundreds of digits

(2) digits below a hundred bits (low bits)

(3) A hundred or more digits (high digit)

 

If the number of hundred digits is 0, the number of times that the hundred digits appear 1 depends on its high digit.

For example, 12013, 1 may be: 100-199 1100-1199 2100-2199 ...... 10100-10199 11100-11199

We can see that when 1 is displayed, the low position (CDE) is 100-199, while the high position (I .e., the AB two) ranges from 0, 1, 2, 3... 11 is exactly 12 pairs, that is, 12 high numbers

So: the number of times that a hundred bits appear 1: The high number 12 × the current number of BITs 100 = 1200

 

If the number of digits is 1, the number of times the number of digits appears 1 depends on the number of digits in the upper and lower positions.

For example, 12113, first according to the above situation: 100-199 1100-1199 2100-2199 ...... 10100-10199 11100-11199

At this time, if the number of BITs is 1, there may be another situation: 12101 12102 12103 ..... 12113 (note that both the height and the height are 12, and the low position ranges from 100 to 113, that is, 13 + 1 = 14 cases)

So: The number of times when a hundred digits appear 1: 12 x the current number of digits: 100 + 13 + 1 = 1214

 

If a hundred-digit number is greater than 1, the number of times a hundred-digit number appears 1 depends on its high-digit number.

For example, 12213, as described in the first case: 100-199 1100-1199 2100-2199 ...... 10100-10199 11100-11199 12100-12199

Note: A hundred-digit number is greater than 1, so do not forget the 12100-12199 case.

So: the number of times that a hundred bits appear 1: (12 + 1) x the current number of BITs 100 = 1300

 

Int onenum (const int num) {assert (Num> = 0); int factor = 1; int COUNT = 0; int curnumber = 0; int highnumber = 0; int lownumber = 0; while (Num/factor! = 0) {lownumber = num-(Num/factor) * factor; // low digit curnumber = (Num/factor) % 10; // current digit highnumber = (Num/factor)/10; // high digit switch (curnumber) {Case 0: Count + = highnumber * factor; break; Case 1: count + = highnumber * factor + lownumber + 1; break; default: Count + = (highnumber + 1) * factor; break;} factor * = 10;} return count ;}

 

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.