[JOBDU] Topic 1373: Number of occurrences in integers 1 (number of occurrences from 1 to n integers 1)

Source: Internet
Author: User

Title Description:

Kiss!! Our foreign friend YZ has not slept well these days , junior high School Olympiad has a problem has been troubled him , hereby he sent to Jobdu to help the letter, hope that the pro can help him. The question is: Find out the number of occurrences of 1 in an integer of 1~13 , and calculate the number of occurrences of 1 in an integer of 100~1300 ? For that, he counted a few times. 1~13 contains 1 of the numbers are 1,10,11,12,13 So there is a total of 6 , but for the latter problem he can not do. Acmer wants you to help him , and to make the problem more common , you can quickly find out the number of occurrences of 1 in any nonnegative integer interval.

Input:

Enter multiple sets of data , one row per set of test data.

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

Output:

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

Sample input:
0 51 1321) 5531 99
Sample output:
1647
Answering:
problem solving problems? to discuss the subject of discussion please visit: http://t.jobdu.com/thread-8096-1-1.html

1#include <iostream>2#include <algorithm>3 using namespacestd;4  5 intA, B;6  7 intGetOne (intN) {8     intCNT =0;9     intm =N;Ten     int Base=1; One      while(M >0) { ACNT + = m/Ten*Base; -         if(m%Ten>1) { -CNT + =Base; the}Else if(m%Ten==1) { -CNT + = n%Base+1; -         } -M/=Ten; +         Base*=Ten; -     } +     returnCNT; A } at   - intMain () { -      while(Cin >> a >>b) { -         if(A >b) Swap (A, b); -cout << GetOne (b)-GetOne (A-1) <<Endl; -     } in     return 0; - } to   + /************************************************************** - problem:1373 the user:hupo250 * language:c++ $ result:acceptedPanax Notoginseng time:40 Ms - memory:1520 KB the ****************************************************************/

[JOBDU] Topic 1373: Number of occurrences in integers 1 (number of occurrences from 1 to n integers 1)

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.