[1-1] statistics problems

'Description of the problem:
the page of a book from a natural number encoded sequentially until a natural number n. Book page layout in accordance with generally customary,
each page can contain extra leading digit 0. For example, on page 6 by the numeral 6, instead of waiting 06 or 006. The number of
word count problems require a given total number of pages of the book n, as many times as the number 0, all the page of the book is used, respectively,
2, ..., 9.
'Programming tasks:
given the total number of pages of a book represented by a decimal integer of n-(1≤n≤109
). All Page programming calculations were used in
the figures of how many times 0,1,2, ..., 9.
'Input data:
the input data file by a text file called input.txt.
Only one row for each document, given the total number of pages of the book represents an integer n.
'Output Results:
At the end of run, outputs the calculation result to the file output.txt. A total of 10 lines of the output file, the k-th row
number k-1 times the output page number used, k = 1,2, ..., 10 .
Input sample output sample files
input.txt output.txt
. 11. 1
. 4
. 1
. 1
. 1
. 1
. 1
. 1
. 1
. 1
[explanations]

000..0 ~ 999..9 (n number 9)
The number of times each number 0 to 9 appear are n * 10 ^ (n-1 ), i.e. the same number of each number appears.
But because without leading 0.
Therefore, 0 to make a multi-operator removed.
Specifically, this number n is set to a length L, the needs to be subtracted 1111 .... 1 (L a 1) 0. excess
and then to be digital DP like.
See: https://www.cnblogs.com/AWCXV/p/7632451.html

[Code]

#include <cstdio>
#include <iostream>
#include <string>
using namespace std;

string s;
int _pow = 1;
int cnt[10];

int get_num(int l,int r){
    int temp = 0;
    for (int i = l;i <= r;i++){
        temp = temp*10+s[i]-'0';
    }
    temp++;
    return temp;
}

int main(){
    cin >> s;
    int len = s.size();
    for (int i = 1;i <= len-1;i++) _pow*=10;
    for (int i = 0;i < len;i++){
        int si = s[i]-'0';
        for (int j = 0;j <= 9;j++) cnt[j]+=si*(len-i-1)*_pow/10;
        for (int j = 0;j <=si-1;j++){
            cnt[j]+=_pow;
        }
        cnt[si]+=get_num(i+1,len-1);
        _pow=_pow/10;
    }
    int more0 = 0;
    for (int i= 1;i <= len;i++){
        more0 = more0*10+1;
    }
    cnt[0]-=more0;
    for (int i = 0;i <= 9;i++){
        cout<<cnt[i]<<endl;
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/AWCXV/p/11615546.html