[Beauty of programming] calculates the number of 1-N records

Source: Internet
Author: User

From: Click me

 

1-digit condition:

In solution 2, we have analyzed that when the value is greater than or equal to 1, there is one, and there is no less than 1.

2 digits:

N = 13, the number of times that a single digit appears is 2, which is 1 and 11 respectively, and the number of times that 10 digits appear 1 is 4, which is 10, 11, 12, 13 respectively, so f (N) = 2 + 4.

N = 23, the number of occurrences of 1 in a single digit is 3, which is, 21, and the number of occurrences of 1 in ten digits is 10, respectively ~ 19, F (n) = 3 + 10.

As a result, we found that the number of single-digit occurrences of 1 is related not only to single-digit, but also to ten-digit numbers. If the single-digit occurrence of 1 is greater than or equal to 1, then the number of single-digit occurrences of 1 is increased by one; if the single digit is 0, the number of times that a single digit appears is equal to 10 digits. The number of occurrences of one in ten digits is not only related to ten digits, but also to single digits. If ten digits are equal to one, the number of occurrences of one in ten digits is 1 in a single digit, if the number of digits is greater than 1, the number of times that one appears on the number of digits is 10.

Three digits:

N = 123

The number of 1 in a single digit is ,..., 91,101,111,121

The number of 10 digits that appear as 1 is 20: 10 ~ 19,110 ~ 119

The number of 1 digits in a hundred bits is ~ 123

We can continue to analyze 4-digit and 5-digit data and export the following general information:

Suppose n, we want to calculate the number of occurrences of 1 in a hundred BITs, which is determined by three parts: the number in a hundred bits, the number in a hundred bits, and the number in a hundred bits.

If the number on the hundred bits is 0, the number of occurrences of 1 on the hundred bits is determined only by a higher value. For example, 12013, the occurrence of 1 on the hundred bits is 100 ~ 199,1100 ~ 1199,2100 ~ 2199 ,..., 11100 ~ 11199. A total of 1200. The value is equal to or greater than the current number, that is, 12*100.

If the number on the hundred bits is greater than 1, the number of occurrences of 1 on the hundred bits is determined only by a higher level. For example, 12213, the occurrence of 1 on the hundred bits is 100 ~ 199,1100 ~ 1199,2100 ~ 2199 ,..., 11100 ~ 11199,12100 ~ 12199: A total of 1300. Equals to a higher digit plus 1 multiplied by the current digit (12 + 1) x 100.

If the number on the hundred bits is 1, the number of occurrences of 1 on the hundred bits is not only affected by the higher level, but also by the lower level. For example, if 12113 is affected by a high position, 1 may occur: 100 ~ 199,1100 ~ 1199,2100 ~ 2199 ,..., 11100 ~ 11199, a total of 1200, but it is still affected by the low level, the case of 1 is 12100 ~ 12113, a total of 114, equal to the low digit 113 + 1.

Based on the above analysis, write the following code:

 1 #include<cstdio> 2 #include<iostream> 3 using namespace std; 4 long CountOne2(long n){ 5     long count = 0; 6     long i = 1; 7     long current = 0,after = 0,before = 0; 8     while((n / i) != 0){            9         current = (n / i) % 10;10         before = n / (i * 10);11         after = n - (n / i) * i;12         if (current > 1)13             count = count + (before + 1) * i;14         else if (current == 0)15             count = count + before * i;16         else if(current == 1)17             count = count + before * i + after + 1;18         i = i * 10;19     }20     return count;            21 }22 int main(){23     long n;24     while((cin >> n)){25         cout << CountOne2(n) << endl;    26     }        27     return 0;28 } 

 

[Beauty of programming] calculates the number of 1-N records

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.