Software Engineering Class Practice--find "one"

Source: Internet
Author: User

First, the topic requirements

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

Requirements:

1. Write a function f (n) and return the number of "1" that appears between 1 and N. For example F (12) = 5.

2. In the range of 32-bit integers, what is the maximum n of "f (n) =n" that satisfies the condition?

Second, design ideas

By enumerating, it is not difficult to find the following laws

1. One decimal number: When N>=1, f (n) = 1; When n=0, f (n) = 0;

2. Two-bit decimal number: f (13) = number of single-digit occurrences of 1 + 10-bit occurrence 1 =2+4=6;

F (23) = digit 1 number + 10 digit appears 1 number =3+10=13;

......

F (93) = digit 1 number + 10 digit appears 1 number =10+10=20;

3. Three-bit decimal number: f (123) = number of occurrences of 1 digit + 10 digit appears 1 number + hundred appears 1 number = (10+10) *1+ (3+10) +24=57;

F (199) =...= (10+10) *2+100;

F (203) =...= (10+10) *2+1+100;

F (213) =...= (10+10) *2+ (2+4) +100;

......

F (303) =...= (10+10) *3+ (1) +100;

......

4.F (ABCDE), calculate the number of 1 on the C-bit, you need to see the situation of AB, C, de:

When c=0, affected by high, the number of hundreds of 1 appear: (AB) *100;

When c=1, affected by high and low, the number of hundreds of 1 appears: (AB) *100+ ((CDE) +1);

When c>1, affected by high, the number of hundreds of 1 appear: ((AB) +1) *100;

Third, the program part of the Code

Part of the code to calculate the number of occurrences of 1

1   while(n/factor!=0)       2     {3lowernum=n-(n/factor) *factor;4curnum= (n/factor)%Ten;5highernum=n/(factor*Ten);6         Switch(Curnum)7         {8          Case 0:9count=count+highernum*factor;Ten              Break; One          Case 1: ACount=count+highernum*factor + Lowernum +1; -              Break; -         default: thecount=count+ (highernum+1)*factor; -              Break; -         } -factor=factor*Ten; +     } -     returncount; +}

Four

V. Summary of the Experiment

According to the teacher's tips to separate the numbers to see, respectively, and 10 bits. "1" in the number of occurrences, listed in turn, it is easy to find the law

Software Engineering Class Practice--find "one"

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.