April 28 Tuesday Classroom Exercise: Number of "1"

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), returns the number of "1" that occurs 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.  Second, design ideas (1) One-time f (0) =0;f (1) =1;f (2-9) = 1;    (2) Two digits F (10) =1+ (0+1) = 2;    F (11) = (+) + (+) = 4;    F (12) = (+) + (2+1) = 5;    F (13) = (+) + (3+1) = 6;    F (23) = (2+1) +10=13;    F (33) = (3+1) +10=14;  F (93) = (9+1) +10=20;    (3) Three digits f (123) = digit number of 1 + 10 digits appearing 1 number + hundred occurrences 1 Number (4) N = ABCDE with C bit as example if (c==0) num=ab*100;    if (c==1) num=ab*100+de+1; if (c>=2) num= (ab+1) *100 three, source program
#include <iostream> #include <math.h>using namespace std;int count1num (int Digit) {int figure=1;// Number of digits in Mark Count 1 (1 digit, 10 10 bit) int curofdigit=0;//current digit int lowerofdigit=0;//lower bit number size (can be multi-bit) int higherofdigit=0;//higher bit number size int Count=0;while (digit/figure!=0) {//Get digital curofdigit= (digit/figure)%10;lowerofdigit=digit-(digit/figure*figure); higherofdigit=digit/(FIGURE*10); if (digit<=0) return 0;if (0==curofdigit)//Current number is 0 o'clock count {count+=higherofdigit* Figure;} else if (1==curofdigit)//The current number is 1 o'clock count {count+=higherofdigit*figure+lowerofdigit+1;} else{count+= (higherofdigit+1) *figure;} figure=figure*10;//number left one}return count;} void Main () {int digit;int max=0;while ((cout<<) Enter the value to be tested (enter-1 end test): "<<endl" && (cin>>digit ) {if (digit==-1) break;cout<< "1 to" <<Digit<< contains 1 numbers: "<<count1num (Digit) <<endl;}}

Four, the operation

V. Summary of the Experiment

The difficulty of the topic is mainly to find the law of "1", and then write the corresponding algorithm according to the finding. The topic is not difficult, but the law is still difficult to find.

April 28 Tuesday Classroom Exercise: Number of "1"

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.