Classroom Practice--Find the number 1 in the decimal number

Source: Internet
Author: User

Title 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, the maximum n of "f (n) =n" satisfying the condition is what. first, the solution of ideasBy enumerating several numbers, we can find that the law of function f (n) is as follows: 1. A 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) = number of digits appearing 1 + 10 digits appear 1 number =10+10=20; 3. Three-bit decimal number: f (123) = The number of single-digit occurrences of 1 + 10 digit appears 1 number + the number of 1 =13+20+24=57; 4.F (ABCDE), calculate the number of 1 on the C-bit, need to see the case of AB, C, de: When c=0, by the high influence, the hundreds appear 1 The number is: (AB) *100 when c=1, affected by high and low, the number of hundreds of 1 appears: (AB) *100+ (CDE) +1) when c>1, by the high influence, the number of 1 on the Hundred is: ((AB) +1) *100 Second, the program code
#include"stdafx.h"#include<iostream.h>#include"stdlib.h"intCountnum (intN) {    intCount=0; intFactor=1; intlowernum=0; intcurnum=0; inthighernum=0;  while(n/factor!=0) {Lowernum=n-(n/factor) *factor; Curnum= (n/factor)%Ten; Highernum=n/(factor*Ten); Switch(curnum) { Case 0: Count=count+highernum*factor;  Break;  Case 1: Count=count+highernum*factor + Lowernum +1;  Break; default: Count=count+ (highernum+1)*factor;  Break; } Factor=factor*Ten; }    returncount;}intMain () {intnum; cout<<"Please enter a number: \ n"; CIN>>num; cout<<"\ n"; cout<<num<<"Number 1 appears in the number: \ n"; cout<<countnum (num) <<Endl; return 0;}

Iii. Results

Iv. ExperienceAt the beginning of the class to get this topic, the first reaction is to find the law, by some examples, special figures to observe, but at the beginning of the time because of too many, their own calculation of the time there was a mistake, very chaotic, and then after the teacher explained and classmates to discuss, find the direction of the law, calm again to calculate, This is to find out its laws, such as the law of the algorithm, must be calm, not irritable, not to give up.

Classroom Practice--Find the number 1 in the decimal number

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.