uva1225digit couting统计

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=93800#problem/C

题意:把前n个整数顺次写在一起,123456789101112…数一数0~9各出现多少次(输出10个整数,分别是0,1,…,9出现的次数)。

#include<iostream>
#include<cstring>
using namespace std;

string str="";
int x[10];

void deal(int n)
{
    string a="";
    for(int i=1;i<=n;i++)
    {
        if(i/1000!=0){a=(i/1000)+'0';str=str+a;}
        if(i/100!=0){a=(i/100)%10+'0';str=str+a;}
        if(i/10!=0){a=(i/10)%10+'0';str=str+a;a=(i%10)+'0';str=str+a;}
        if(i/10==0){a=i+'0';str=str+a;}
    }
}

void solve()
{
    int len=strlen(str.c_str());
    for(int i=0;i<len;i++)
    {
        if(str[i]=='0')x[0]++;
        if(str[i]=='1')x[1]++;
        if(str[i]=='2')x[2]++;
        if(str[i]=='3')x[3]++;
        if(str[i]=='4')x[4]++;
        if(str[i]=='5')x[5]++;
        if(str[i]=='6')x[6]++;
        if(str[i]=='7')x[7]++;
        if(str[i]=='8')x[8]++;
        if(str[i]=='9')x[9]++;
    }
    for(int i=0;i<10;i++)
    {
        cout<<x[i];
        if(i<9)cout<<" ";
    }
    cout<<endl;
}

int main()
{
    int t;
    cin>>t;
    memset(x,0,sizeof(x));
    while(t--)
    {
        int n;
        cin>>n;
        deal(n);
        solve();
        str="";
        memset(x,0,sizeof(x));
    }
    return 0;
}

 

posted @ 2016-05-07 10:44  哲贤  阅读(162)  评论(0编辑  收藏  举报