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)