UVa1225 DIgit Counting

来源:互联网 发布:java string split 点 编辑:程序博客网 时间:2024/05/22 02:03

Problem Description

把前n(n<=10000)个整数顺次写在一起:例如12: 123456789101112 数一数0-9各出现多少次

Sample Input

2
3
13

Sample Output

0 1 1 1 0 0 0 0 0 0
1 6 2 2 1 1 1 1 1 1

代码:

#include<stdio.h>#include<string.h>char a[10005][100005];char s[1000000];int main(){    int t, i, n, b[15];    scanf("%d", &t);    while(t--)    {        memset(a, 0, sizeof(a));        memset(s, 0, sizeof(s));        memset(b, 0, sizeof(b));        scanf("%d", &n);        for(i = 1; i <= n; i++)        {            sprintf(a[i - 1], "%d", i);//打印到字符串        }        for(i = 0; i < n; i++)        {            strcat(s, a[i]);//连接成一个串        }        for(i = 0; s[i]; i++)        {            b[s[i] - '0']++;//记录数据        }        for(i = 0; i < 9; i++)        {            printf("%d ", b[i]);        }        printf("%d\n", b[i]);    }    return 0;}
0 0