The number of "1" for classroom practice

Source: Internet
Author: User

First, the topic

Given a positive decimal integer, write down all integers starting at 1, to N, and then count the number of 1.

Second, the requirements

1. Write a function f (N) and return the number of "1" between 1~n, for example: f (12) = 5;

2, in the 32-bit integer range, satisfies the condition "F (N) =n" The maximum N is how much;

Third, the idea

Case 1: If the number on the hundred is 0, then you can know that the hundreds may occur 1 times higher than the number of decisions, such as 12 013, you can be aware that the hundred 1 of the situation may be 100-199,1 100-1 199,......,11 100-11 199, a total of 1 200 of them. That is, it is determined by a higher number (12) and is equal to the number of digits (100) of the higher digit X.

Case 2: If the number on the hundred is 1, then you can know that the hundreds may occur 1 times not only by the higher impact, but also by the low impact, that is, higher and lower levels of 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.

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

Iv. source code of the procedure

1#include <iostream.h>2 3 Long intFind (intN)4 {5     Long intCount=0;//Number of 16     Long intf=1;//Current Bit7     Long intlnum=0;//Low number8     Long intcnum=0;//Current number9     Long inthnum=0;//High numberTen     if(n<=0) One     { A         return 0; -     } -      while(n/f!=0) the     { -lnum=n-(n/f) *f;//Low number -cnum= (n/f)%Ten;//Current number -hnum=n/(f*Ten);//High number +         if(cnum==0) -         { +Count +=hnum*F; A         } at         Else if(cnum==1) -         { -count+=hnum*f+lnum+1; -         } -         Else -         { incount+= (hnum+1)*F; -         } tof*=Ten; +     } -     returncount; the } *  $ intMain ()Panax Notoginseng { -     Long intN; thecout<<"Please enter a number n (n>0):"; +Cin>>N; Acout<<"the number of 1 is:"<<find (n) <<Endl; the     return 0; +}

Five, the operation

Vi. Summary of the experiment

Through this classroom contact, let me learn in the encounter similar problems should be first examples of operations, and in the calculation process to find the law, according to find the law

Code to achieve the requirements of the topic.

The number of "1" for classroom practice

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.