Software Engineering class Exercises--Find out the number of 1 occurrences in 1-n

Source: Internet
Author: User

Title: Given a positive integer in decimal, write down all integers starting with 1, to N, and then count the number of "1" that appears.

Requirement: Write a function f (n) to return the number of "1" occurrences between 1 and N.   For example F (12) = 5. Within a 32-bit integer range, the maximum n of the "F (n) =n" that satisfies the condition is what. First, the design idea through the induction method, you can find:

Suppose n = abcde, where a,b,c,d,e are numbers on each digit of the decimal number n respectively. If you want to calculate the number of occurrences of 1 on the Hundred, it will be affected by 3 factors: the number on the hundred, the number below the Hundred (low), the number of the hundred (more high).

If the number on the hundred is 0, you can know that the hundred may appear 1 times higher than the number of decisions, such as 12 013, you can know that the hundred 1 of the situation may be 100-199,1 100-1 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 number of digits (100) of the higher digit X.

If the number on the hundred is 1, then it is known that the number of possible 1 on the hundred is not only affected by the higher level, but also by the low impact, that is, the higher and the lower the joint decision. For example 12 113, affected by a higher level, the hundred appear 1 of the case is 100-199,1 100-1 199,......,11 100-11 199, a total of 1 200, and the first case above, equal to the higher digits (100) x the current number of digits. But it is also affected by the low, hundreds of 1 of the situation is 12 100-12 113, a total of 114, equal to the low number (113) +1.

If the number on the hundred is greater than 1 (that is, 2-9), then the hundreds may appear on the number of 1 is also determined by a higher level, such as 12 213, the Hundred 1 is the case: 100-199,1 100-1 199,......,11 100-11 199,12 100-12 199, a total of 1300, and equals the higher number +1 (12+1) x current digits (100).

Second, the source code

1  PackageCom.java.lianxi;2 3 ImportJava.util.Scanner;4 5  Public classLianxi9 {6      Public Static voidMain (string[] args)7     {8Scanner cin=NewScanner (system.in);9System.out.print ("Please enter the maximum value of a positive integer:");Ten         intn=cin.nextint (); OneSystem.out.print ("1 of the number is:" +sum1s (n)); A     } -      Public Static intSum1s (intN) -     {   the         intICount = 0;  -         intIfactor = 1;  -        -         intIlowernum = 0;  +         intIcurrnum = 0;  -         intIhighernum = 0;  +        A          while(N/ifactor! = 0 )   at         {   -Ilowernum = N-(n/ifactor) *Ifactor;  -Icurrnum = (n/ifactor)% 10;  -Ihighernum = N/(ifactor *10 );  -        -             Switch(Icurrnum) in             {   -              Case0:   toICount + = Ihighernum *Ifactor;  +                  Break;  -              Case1:   theICount + = Ihighernum * ifactor + ilowernum + 1;  *                  Break;  $             default:  Panax NotoginsengICount + = (ihighernum + 1) *Ifactor;  -                  Break;  the             }   +        AIfactor *= 10;  the         }   +         returnICount;  -     }   $}

Iii. Results of operation

Iv. Experience

The idea of this exercise, and mathematical induction is very similar, the saying is to find the law, through the list of different circumstances, found the law, thus obtained the solution, which is a special to the general idea.

Software Engineering class Exercises--Find out the number of 1 occurrences in 1-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.