Number of 1 (Programming beauty Java version) __ Programming

Source: Internet
Author: User

Programming Beauty 2.4 (number of 1)

With a careful analysis of the problem, given N, it appears that the sum of the "number of times less than n" may appear on each of the 1 "and come to this result." Let's take a look at how to get a rule for a particular N to analyze all the possibilities of 1 on each one, and to sum the last f (n).

Start by looking at some simple situations and see if you can sum up the rules.

Let's look at the 1-digit number first.

If N = 3, then all numbers from 1 to 3 are: 1, 2, 3, only single-digit digits may appear 1, and only 1 times, further can be found if N is Single-digit, if n>=1, then f (n) is equal to 1, if n=0, then f (n) is 0.

Let's look at the 2-digit number.

If n=13, then all numbers from 1 to 13:1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, single-digit and 10 digits may have 1, we can separate them to consider, a single-digit 1 times two: 1 and 11, 10 digits appear 1 times 4:10, 11, 12 and 13, so F (N) =2+4=6. Note that the number 11 in both 10 and a bit has appeared 1, but 11 is just in the Single-digit 1 and 10 in 1 is calculated two times, so no special treatment, is right. Then consider the situation of n=23, it and n=13 a bit different, 10-bit 1 times 10 times, from 10 to 19, Single-digit 1 of the number of times 1, 11 and 21, so F (N) =3+10=13. By analyzing the two digits, we found that 1 of the number of single-digit digits is not only related to single-digit digits, also related to the 10-digit number: If n's single-digit number is greater than or equal to 1, the number of digits appearing at 1 is 10 digits plus 1; if n is single-digit 0, a digit of 1 is equal to 10 digits in number. and the number of 1 on the 10-digit number is not only related to the 10-digit number, also related to single-digit digits: If the 10 digits equals 1, the number of 1 on the 10-digit number is single-digit plus 1, or 10 if the 1-digit number is greater than 10.

F (13) = The number of 1 in Single-digit + 10 bits appearing 1 = 2 + 4 = 6;

F (23) = The number of 1 in Single-digit + 10 bits appearing 1 = 3 + 10 = 13;

F (33) = The number of 1 in Single-digit + 10 bits appearing 1 = 4 + 10 = 14;

...

F (93) = the number of 1 in Single-digit + 10 bits appearing 1 = 10 + 10 = 20;

Then analyze the 3-digit number.

If n = 123:

The number of digits appearing at 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 appearing 1 is 24:100~123

F (23) = Single-digit Number of 1 + 10 digits appearing 1 + hundred appearing 1 times = 13 + 20 + 24 = 57;

Similarly, we can analyze the 4-digit, 5-digit number again. Readers can write a summary of what is different.

Based on some of the previous attempts, we derive a general calculation of F (n) from N:

Suppose N=abcde, where a, B, C, D, and e are numbers on each digit of the decimal number n. If you want to calculate the number of 1 on the Hundred, it will be affected by three factors: the number on the hundred, the number below the Hundred (low), and the number above the Hundred (higher).

If the number on the hundred is 0, you can know that the number of hundreds of 1 can be determined by a higher level, such as 12 013, you can know that the hundreds of 1 may be 100~199,1 100~1 199,2 100~2 199,...,11 100~11 199, a total of 1 200. That is, it is determined by a higher number (12) and is equal to the current number of digits (100) of the higher position number (x).

If the number on the hundred is 1, it can be seen that the number of 1 on the hundred is not only affected by the higher level, but also by the low impact, that is, the higher and lower the common decision. For example, for 12 113, the number of hundreds appearing 1 is 100~199,1 100~1 199,2 100~2 199,...,11 100~11 199, altogether 1 200, as in the first case above, equal to the current digit (100) of the higher digits (x). But it is also affected by the low, 1 of the situation is 12 100~12 113, a total of 114, equal to the low number (123) +1.

If the number on the hundred is greater than 1 (that is, 2~9), the number of 1 that may occur on the hundred is determined only by a higher level, such as 12 213, and the probability of a hundred 1 is: 100~199,1 100~1 199,2 100~2 199,...,11 100~11 199,12 199, a total of 1 300, and equal to the higher number +1 (12+1) x current digits (100).

By summarizing and summarizing the above, we can write the following more efficient algorithm to compute F (N):

Code Listing 2-10

int count=0;//need to be aware of Cross-border int number=1;//operands
int lower=0;//single digit 1 digits
int mid=0;//10-bit 1-digit
int high=0;//hundred 1 digits
int n=123;
while (n/number!=0)
{
lower=n-(N/number) *number;
Mid= (N/number)%10;
high=n/(NUMBER*10);
System.out.println (lower+ "mid=" +mid+ "High" +high);
Switch (mid)
{
Case 0:
Count+=high*number;
Break
Case 1:
count+=high*number+lower+1;
Break
Default
count+= (high+1) *number;
Break
}
number=number*10;
}
System.out.println (count);

}



2

F (n) =n;

The beauty of programming is much more cumbersome to store

As for the 10 of the 11-time side of the large number, beyond the long range, so need a large number of storage, using string to solve

This method requires the reader to parse

Now just verify the programming beauty of the results given (c + +)

1111111110

#include <iostream>


using namespace std;


Int main ()
{
          Long long int low;//Note cross-border problems
 long Long int mid;
 long long int high;
 long Long int number = 1;
 long long int n=1111111110;
 long Long int count = 0;//1 count
while (n/number!=0)
{
low=n-(n/number) *number;
Mid= (N/number)%10;
high=n/(NUMBER*10);
Switch (mid)
{
Case 0:
Count+=high*number;
Break
Case 1:
count+=high*number+low+1
Break
Default:
count+= (high+1) *number;
Break


}
number*=10;
}
if (count==n)
{
           cout<<count<<endl
}


    return 0;
}

Related Article

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.