To find the number of occurrences of 1 in an integer

Source: Internet
Author: User
Tags count integer

Topic Description:

Kiss them!! Our foreign friends yz these days are always bad, junior high School Olympiad has a topic has been bothering him, hereby he sent to JOBDU for help letter, hope to help him. The question is: Find the number of occurrences of 1 in a 1~13 integer and figure out how many 1 occurrences of the 100~1300 integer. He counted it for a special reason. 1~13 contains 1 of the number 1, 10, 11, 12, 13, so there are 6, but for the back of the problem he has no way. Acmer wants you to help him, and to make the problem more universal, you can quickly find out the number of 1 occurrences in any nonnegative integer interval.

Input:

Enter multiple sets of data, each set of test data as one row.

Each row has two integers a,b (0<=a,b<=1,000,000,000).

Output:

For each test case, output the number of 1 occurrences between A and B.

Sample input:

0 5

1 13

21 55

31 99

Sample output:

1

6

4

7

Idea: We first write a function to find the number of occurrences of 1 from 1 to the integer n. Then we will require the input of the two number (specifically, should be the largest number, and the smallest number minus 1) as a parameter passed into the function, the resulting value subtraction, you can get the number of the two between the 1 occurrences.

The simplest method is to find the number of 1 of each number from 1 to N, respectively, since the number of digits of the integer n is o (logn), we have to determine how many 1 of a number we need to determine whether each digit is 1, so that a number needs to be judged O (Logn), and a total of n is required, Then the time complexity of the method is O (Nlogn). Code tests written with this method on a nine-degree OJ will time out.

Sword refers to the offer on a recursive thinking, can reduce the complexity of time to O (Logn), the general feeling a bit biased, and it is difficult to think, I did not look carefully. My idea is that you separate statistics, and see He Haitao blog Below a lot of people leave a message, also used such a method, it looked at the detailed ideas, and deduced the formula, wrote the code, feeling that this method is very nice, intuitive and easy to understand, and the code concise, time complexity of the same O (Logn).

Back to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

This method of thinking is probably like this (do not bother to play, direct copy):

Consider each one,

1 This bit is greater than 1, the number on this one 1 is ([n/10^ (b+1)] + 1) * 10^b

2 This bit equals 0, for ([n/10^ (B+1)]) * 10^b

3 This bit equals 1, on the basis of 0 plus n mod 10^b + 1

As an example:

30143:

As a result of the 3>1, the number of digits appearing at 1 is (3014+1) *1

Because of 4>1, the number of 1 on 10 digits is (301+1) *10

As a result of 1=1, 1 times (30+0) *100+ (43+1) appear on the Hundred

As a result of 0<1, 1 times (3+0) are present on the thousand *1000

Note: In the case of hundreds, hundreds of 1 of 100~199,*100 meaning for the single step appeared 100~199,100 times, *30 is because 30 times 100~199,+ (43+1) is due to the left after the 301** incomplete.

If you do not understand, you take paper and pen roughly write down, find the law, you can deduce it!

Two things to be aware of: because the test system requires the maximum input data of 1,000,000,000, so the use of int will overflow, to use long long, the other point of comparison of the crater is a may be larger than B, it is not explained, a bit of a pit.

The AC code is as follows:

#include <stdio.h>///* Statistics num 1 occurrences of each number, add to get 1 total number of occurrences/long long CountNum1 (long long num) {if nu  
     
    M <= 0) return 0;    Long Long count = 0;    Statistic 1 occurrences of the number of long long current; Current bit long long base = 1;   The base long long remain = 0 of the current bit;  
        The current bit is 1 o'clock and the number of remaining digits (that is, the incomplete portion) in the back bit is 1 times while (num) {current = num%10;  
     
        num = NUM/10;  
        if (current > 1) Count + = (num+1) *base;  
        else if (current = = 1) Count + = Num*base + (remain+1);  
         
        else count = num*base;   
        The next one to use the base and the remaining incomplete part value remain + = Current*base;  
    Base *= 10;  
return count;  
    int main () {Long long a,b;  
        The size of the A,b (scanf ("%lld%lld", &a,&b)!= EOF) {Long long result;  
        if (a > b) result = COUNTNUM1 (a)-CountNum1 (b-1); else result = COUNTNUM1 (b)-CountNum1 (A-1);  
    printf ("%lld\n", result);  
return 0; }

Source: http://blog.csdn.net/ns_code/article/details/27563485

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.