Number of "1"

Source: Internet
Author: User

Experimental topics

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

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;

Experimental ideas

First, analyze the law

F (3) =1

F (13) =2+4=6

F (19) =2+10=12

F (23) =3+10

F (33) =4+10

F (93) =10+10=20

F (123) =24+20+13=57

Set n = ABCDE, where abcde are the numbers on each of the decimal members.

If you want to calculate the number of occurrences of 1 in the hundreds, it will be affected by 3 aspects: the number on the hundred, the number on the Hundred (low), the number on the Hundred (high).

If the number on the hundred is 0, the hundred may appear 1 times higher. For example: 12013, you can know that the hundred 1 of the situation may be: 100~199,1100~1199,2100~2199,,.........,11100~11199, altogether 1200. It can be seen that the higher number (12) is determined and is equal to the higher number (12) multiplied by the current number of digits (100).

If the number on the hundred is 1, the number of possible 1 on the hundred will be affected not only by higher levels but also by low levels. For example: 12113, you can know that the hundred affected by the high position is: 100~199,1100~1199,2100~2199,,.........,11100~11199, altogether 1200. The same as above, and equals the higher number (12) multiplied by the current number of digits (100). But at the same time it is also affected by low, hundreds of 1 of the situation is: 12100~12113, a total of 114, equal to the low number (113) +1.

If the number on the hundred is greater than 1 (2~9), then 1 of the situation on the hundred is determined by a higher position, such as 12213, then 1 of the Hundred occurrences are: 100~199,1100~1199,2100~2199,...........,11100~11199,12100 ~12199, a total of 1300, and equals a higher number +1 (12+1) multiplied by the current number of digits (100).

Experiment Code

#include "stdafx.h" #include "iostream" using namespace Std;int Main () {    int a;//input positive integer    int count=0;//Count 1    int i=1;//low digit    int m=0;//Current bit-digit    int n=0;//high-level digit    int b=0;    cout<< "Please enter a positive integer:" <<endl;    cin>>a;    while (a/i!= 0)    {         m=a-(a/i) *i;        n= (a/i)%;         b=a/(i*10);        if (n==0)        {            count+=b*i;        }        else if (n==1)        {            count+=b*i+m+1;        }        else        {            count+= (b+1) *i;        }        i*=10;    }    cout<< "The number of the positive integer 1 is:" <<count<<endl;    return 0;}

Experiment Experience

The idea of the experiment took a long time, obviously the teacher said is very simple topic, but also spend so many hours a little should not. I think the problem is that the problem is to classify the situation too much, paper down to slowly analyze the results can be made, feeling a sense of accomplishment.

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.