Classroom exercises-Finding the number of 1 occurrences

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. 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.

Second, the design idea

The first feeling to see this problem is that you can use the most basic way to find out 1 occurrences of the number of times, but we can find out from here the law, a number contains 1 of the number of occurrences of the number of times, the number of 10 occurrences, the number of occurrences of the total number of hundred Through analysis can know if the number of single-digit occurrences of the number of times only two cases 0 and 1, if two bits dozens of bits is not 0 then the number of digits 1 is 10, 1 is the number 10 on the 1 bit, and the number of digits on the top is 10. The number of occurrences is digit plus 1. Occurs 10 times if it is greater than a. It can be seen that each person appears 1 times with high, low and itself has a certain relationship. The sum can be obtained, the decimal number ABCDE calculates the number of hundreds (c) appear 1 times, if the hundred is 0, then the number of 1 on the Hundred is (AB) *100, if the Hundred is 1, then the number of 1 on the Hundred is (AB) *100+cde+ 1, if the number on the hundred is greater than 1. The number of 1 on the Hundred is (ab+1) *100.

Third, the source code
#include <iostream.h>intFindintnum) {    intCount=0;//define a variable that stores 1 of the number    intTemp//to define a cached variable     for(intI=1; i<=num;i++) {Temp=i;  while(temp!=0)        {            if(temp%Ten==1) {Count++; } temp=temp/Ten; }    }    returncount;} intMainvoid){    intNUM1; cout<<"Please enter the number:"; CIN>>NUM1; cout<<"the number that contains 1 is:"<<find (NUM1) <<Endl; return 0;}

Iv. Results

V. Summary

Through this exercise, I understand the importance of programming ideas, you want to write a program, whether large or small, you have a good idea to solve the problem, when you think of a plan, don't be happy too early, you have to think about there is no better way to solve the problem, or how to optimize the present scheme, These are the problems that become simple and clear!

Classroom Practice-finding the number of occurrences 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.