1049. Counting ones/number of occurrences in integer 1 (number of occurrences from 1 to n integers 1)

Source: Internet
Author: User

The task is simple:given any positive an integer N, you're supposed to count the total number of 1 's in the decimal form of The integers from 1 to N. For example, given N being, there is five 1 ' in 1, ten, one, and 12.

Input Specification:

Each input file contains one test case which gives the positive N (<=230).

Output Specification:

For each test case, print the number of 1 's in one line.

Sample Input:

12

Sample Output:

5



The title describes the number of occurrences of 1 in an integer of 1~13, and calculates 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.
1 classSolution {2  Public:3     intCountone (std::stringnum)4     {5         intFIR = num[0] -'0';6         if(Fir = =0)7             return 0;8         intLen =num.length ();9         if(len = =1)Ten             return 1; One         intnum1,num2,num3; A         if(Fir = =1) -         { -             stringRe =num, tem; the num.erase (Num.begin ()); -TEM =num; -num =re; -Re =tem; + StringStream SS; -SS <<re; +SS >>NUM1; A++NUM1; at         } -         Else -         { -NUM1 = Pow ((Double)Ten, Len-1); -         } -  in  -num2 = Fir * (len-1) * POW ((Double)Ten, len-2); to         stringTnum =num; + tnum.erase (Tnum.begin ()); -NUM3 =Countone (tnum); the         returnNUM1 + num2 +num3; *     } $     intNumberof1between1andn_solution (intN)Panax Notoginseng     { -         if(n = =0) the             return 0; +         if(N <Ten) A             return 1; the Std::stringstream SS; +STD::stringnum; -SS <<N; $SS >>num; $         returncountone (num); -     } -};

1049. Counting ones/number of occurrences in integer 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.