Enter an integer n to calculate the number of times 1 appears in the decimal representation of the N integers from 1 to n.

Source: Internet
Author: User

Question:

Enter an integer n to calculate the number of times 1 appears in the decimal representation of the N integers from 1 to n.

For example, input 12, integers from 1 to 12 contain numbers 1, 10, 11, and 12, and 1 appear 5 times in total.

If n = 23106 is required, the calculation process is as follows:

Tens of thousands = 1:10000 --19999 total 10000

Thousands = 1:01000--21999 for a total of 3*1000 = 3000, of which 3 indicates that 1000 bits can be filled in 1000 and 2. 3000 indicates that the last three bits have to possibilities, so there are possibilities in total.

Hundred bits = 1:The possible situation here is not: 00100--23199, because the maximum is only 23106, we can divide the sum into two parts:

00100--22199: a total of 23*100 types, of which 23 indicates thousands of digits can take 00-100 23 kinds of possibilities, 99,100 indicates that ten digits can take 00-possible.

23100--23106: a total of 7 types, that is, the thousands of digits are fixed. In only 23 cases, the ten digits can be 00-56, a total of 57 possibilities.

Therefore, the number of digits with a hundred digits being 1 is 2300 + 7 = 2307;

10 digits = 1:00010--23019 A total of 231*10 = 2310 types, of which 231 indicates that thousands of hundred digits can take 231,232-kinds of possibilities, while 10 indicates that a single digit can go 0-9 ten possibilities.

Because it takes 23110> 23106, so thousands of hundred digits = 231, and the number of digits 10 = 1 is 0

Single digit = 1:00001-- 23101A total of 2311*1

So the total number: 10000 + 3000 + 2307 + 2310 + 2311 = 19928 possibilities.

If a five-digit number is ABCDE (AB is definitely not equal to 0) and we want to calculate the number of numbers C = 1, we can discuss it in the following situations:

C> 1:00100 -- AB199, a total of (AB + 1) x 100 types, of which (AB + 1) indicates that thousands of digits can be 00-AB, a total of AB + 1 type. Because c> 1, so ab199 <ABCDE, so these numbers are within the range of 1 -- ABCDE.

C = 0:00100 -- A (b-1)199, a total of AB * 100, and A (b-1) between 200-ab099, There is no number of hundred = 1.

C = 1:We can divide the number C = 1 into the sum of the following two cases:

00100 -- A (b-1)199: A total of AB * 100 possibilities;

A (b-1)200 -- AB 0 99: the number of digits with a hundred digits = 1 is 0;

AB 1 00 -- AB 1 CD: total Cd + 1 possibility;

Therefore, the number of numbers C = 1 is equal to the number of numbers C = 0, plus ABCDE % 100 + 1.

So we only need to discuss each digit as shown above, get the number of this digit = 1, and then accumulate it to get the answer to the question lock.

#include <iostream>using namespace std; int num_of_1(int n){    /*it should cover all powers of 10 within int range*/    int pow1 = 1;    int pow2 = 10*pow1;    int  count = 0;    while(n >= pow1){//        int pow2 = 10*pow1;        switch( (n % pow2) / pow1){                case 0:                    count += (n / pow2) * pow1;                    break;                case 1:                    count += (n / pow2) * pow1;                    count += n % pow1 + 1;                    break;                default:                    count += (n / pow2 + 1) * pow1;        }        pow1 *= 10;    }    return count;} int main(){    cout << num_of_1(12) << endl;    getchar();    return 0;}

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.