The beauty of programming is 2.4, and the beauty of programming is 2.4.

Source: Internet
Author: User

The beauty of programming is 2.4, and the beauty of programming is 2.4.

Given a number n, 1 appears several times in the number n from 1 to n. At the beginning of this question, it is certainly not easy to do. It is often the most stupid way to find a single number. If n is large, it will take a lot of time, the book provides a better way to find the relevant laws in numbers:

For the number of abcde, the number of occurrences of 1 in c is as follows: 1. if c> 1, the conclusion is (AB + 1) x 100; 2. if c = 1, the conclusion is (AB) * 100 + de + 1; 3. if c <1, the final wheel is AB * 100 + de + 1;

The above means that each occurrence of 1 is related to its high position and low position. Therefore, we only need to obtain the current position, high position, and low position. Therefore, first, define the function:

/* Number of 1 in 2.4 */int DutCountOf1InN (int );

Then, we use the formula above to easily write the following simple code:

/** For the number of abcde, the number of occurrences of 1 in c is as follows: * 1. if c> 1, the conclusion is (AB + 1) x 100; * 2. if c = 1, the conclusion is (AB) * 100 + de + 1; * 3. if c <1, the final wheel is AB * 100 + de + 1; */int DutCountOf1InN (int n) {if (n <= 0) return 0; int count = 0; /* multiplier factor, increasing by 10 times */int factor = 1; int lowerNum = 0; int currNum = 0; int highNum = 0; while (n/factor! = 0) {/* low digit */lowerNum = n-(n/factor) * factor;/* Current digit */currNum = (n/factor) % 10; /* high digit */highNum = n/(factor * 10); switch (currNum) {case 0: count + = highNum * factor; break; case 1: count + = highNum * factor + lowerNum + 1; break; default: count + = (highNum + 1) * factor; break;} factor * = 10;} return count ;}

This question is not over yet. If the question is not about the number of 1, it is about 2, 3 .... Is the number of instances the same solution? After analysis, we found that we can still use the formula above, so we can use the following code to get the number of c. Of course, c belongs to []:

Function Declaration

/* Number of c */int DutCountOfCInN (int, int );


Source code:

/** For the number of abmde, the number of occurrences of c in m is as follows: * 1. if m> c, the conclusion is (AB + 1) x 100; * 2. if m = c, the conclusion is (AB) * 100 + de + 1; * 3. if m <c, the final wheel is AB * 100 + de + 1; */int DutCountOfCInN (int N, int c) {if (N <= 0) return 0; int sum = 0; int low = 0, cur = 0, high = 0; int ifactor = 1; while (N/ifactor! = 0) {low = N-(N/ifactor) * ifactor; cur = (N/ifactor) % 10; high = (N/ifactor)/10; if (cur <c) {sum + = high * ifactor;} else if (cur = c) {sum + = high * ifactor + low + 1 ;} else {sum + = (high + 1) * ifactor;} ifactor * = 10;} return sum ;}





124711 ...... In this case, what is the number of 25th?

1 = 1 + 0
2 = 1 + 1
4 = 1 + 1 + 2
7 = 1 + 1 + 2 + 3
11 = 1 + 1 + 2 + 3 + 4
......
An = 1 + [1 + 2 + ...... + (N-1)] = 1 + n (n-1)/2
A25 = 1 + 25*24/2 = 301

Which is the ratio of 18 to 24?





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.