Count 1 between 0 and N

Source: Internet
Author: User

Descriptive narrative of the problem
Given a decimal integer n, the number of "1" appears in all integers from 1 to n.

For example: 1 "1" appeared in n=2.

n=12 when 1,2,3,4,5,6,7,8,9,10,11,12.

There were 5 "1".

Thinking of solving problems

1-digit situation:

In the solution two has been analyzed, greater than or equal to 1 times. There are 1, less than 1 there is no.

2-digit situation:

N=13, the number of single-digit occurrences of 1 is 2. 1 and 11, 10 digits 1 are 4, respectively 10,11,12,13, so f (N) = 2+4.

N=23, the number of single-digit occurrences of 1 is 3, respectively, 1,11,21, 10 digits 1 of the number of 10, respectively, 10~19,f (N) =3+10.

As a result, we find. Single-digit occurrences of 1 are not only related to single-digit numbers. and 10 digits are also concerned, assuming that the single digit is greater than or equal to 1, the number of single-digit 1 is 10-digit number plus 1, assuming that the single digit is 0. Single-digit occurrences of 1 are equal to 10-digit numbers. The number of 10-digit occurrences of 1 is not only related to the 10-digit number, but also to the single-digit: Assuming that the 10-digit number equals 1, 10 on the 1-digit number is the digit of the single digit plus 1, if the 10-digit number is greater than 1, 10 is 1 on the 10-digit number.

3-digit situation:

N=123

The number of single-digit occurrences of 1 is 13:1,11,21, .... 91,101,111,121

The number of 10-bit occurrences of 1 is 20:10~19,110~119

The number of hundreds of 1 appears to be 24:100~123

We can continue to analyze 4-digit, 5-digit numbers and derive the following general conditions:

Suppose N, we want to calculate the number of occurrences of 1 on the Hundred. will be decided by three parts: the number on the hundred, the number above the hundred. Hundreds of numbers.

Assuming that the number on the hundred is 0, then the number of occurrences of 1 on the hundred is determined only by a higher level, for example 12013, where 1 of the hundreds appear as 100~199,1100~1199,2100~2199,...,11100~11199, a total of 1200. equals the higher number multiplied by the current number of digits. That is 12 * 100.

Assuming that the number on the hundred is greater than 1, the number of occurrences of 1 on the hundred is determined only by the higher. For example, 12213, hundreds of 1 of cases are 100~199,1100~1199,2100~2199. ..., 11100~11199. 12100~12199 a total of 1300.

equals the higher number plus 1 times the current number of digits, i.e. (12 + 1) *100.

Assuming that the number on the hundred is 1, the number of occurrences of 1 on hundreds is not only affected by higher levels, but also by low levels. For example, 12113, is affected by high level 1 of the situation: 100~199,1100~1199,2100~2199. ..., 11100~11199, a total of 1200, but it is also affected by low, 1 of the situation is 12100~12113, a total of 114, equal to the low number 113+1.

Implementation code
#include <iostream>#include <cstring>using namespace STD;intCountone (intN) {intCurrent =0;intBefore =0;intafter =0;inti =1;intCount =0; while(n/i! =0) {current = n/i%Ten; Before = n/(i *Ten); after = N-(n/i) * i;if(Current >1) {count + = (before +1) * I; }Else if(Current = =0) {count + = before * I; }Else if(Current = =1) {count + = before * i + after +1; } I *=Ten; }returnCount;}intMain () {intN while(Cin>>n) {cout<<countone (n) <<endl; }return 0;}

Count 1 between 0 and N

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.