UVa1225Digit Counting(计算1-n的整数0-9各出现了多少次,神级代码...)

来源:互联网 发布:软件开发公司网站 编辑:程序博客网 时间:2024/06/04 18:30


UVA - 1225
Digit Counting

Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

SubmitStatus

Description

Download as PDF

Trung is bored with his mathematics homeworks. He takes a piece of chalk and starts writing a sequence of consecutive integers starting with 1 toN(1 <N < 10000) . After that, he counts the number of times each digit (0 to 9) appears in the sequence. For example, withN = 13 , the sequence is:

12345678910111213

In this sequence, 0 appears once, 1 appears 6 times, 2 appears 2 times, 3 appears 3 times, and each digit from 4 to 9 appears once. After playing for a while, Trung gets bored again. He now wants to write a program to do this for him. Your task is to help him with writing this program.

Input 

The input file consists of several data sets. The first line of the input file contains the number of data sets which is a positive integer and is not bigger than 20. The following lines describe the data sets.

For each test case, there is one single line containing the number N .

Output 

For each test case, write sequentially in one line the number of digit 0, 1,...9 separated by a space.

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

原题链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=27516
题意:
把前n(n<=10000)个整数顺次写在一起,如n=15时,123456789101112131415
计算0-9各出现了多少次(输出10个数,分别是数字0-9出现的次数)

我不说话,就静静的看代码...

AC代码:
#include<cstdio>int a[10000][10];int main(){    for(int i=1;i<10000;i++)    {        for(int j=0;j<10;j++)            a[i][j]=a[i-1][j];        for(int k=i;k;k/=10)            a[i][k%10]++;    }    int n;    scanf("%d",&n);    for(int i=0;i<n;i++)    {        int num;        scanf("%d",&num);        for(int j=0;j<9;j++)            printf("%d ",a[num][j]);        printf("%d\n",a[i][9]);    }    return 0;}

另外还有一份代码:
#include<stdio.h>#include<string.h>#include<ctype.h>#define max 40000+10char s[max];int main(){    int T;    scanf("%d", &T);    while (T--)    {        int n;        int first = 1;        scanf("%d", &n);        char*p = s;        for (int i = 1; i <= n; i++)        {            sprintf(p, "%d", i);            if (i < 10) p++;//指针移动的位数要随着数字位数而调整            else if (i < 100) p += 2;            else if (i < 1000) p += 3;            else if (i < 10000) p += 4;            else p += 5;        }        int a[10];        memset(a, 0, sizeof(a));        int len = strlen(s);        for (int i = 0; i < len; i++)        {            a[s[i]-'0']++;//源代码网上看到的,觉得Ta用switch太啰嗦,遂把下面的注释不封改成了这句            /**            switch (s[i])            {            case'0':          <span style="white-space:pre"></span>a[0]++;                break;            case'1':                a[1]++;                break;            case'2':                a[2]++;                break;            case'3':                a[3]++;                break;            case'4':                a[4]++;                break;            case'5':                a[5]++;                break;            case'6':                a[6]++;                break;            case'7':                a[7]++;                break;            case'8':                a[8]++;                break;            case'9':                a[9]++;                break;            }            */        }        for (int i = 0; i < 10; i++)        {            if (first)           //注意第一个字符处不要有空格                first = 0;            else                printf(" ");            printf("%d", a[i]);        }        printf("\n");    }    return 0;}



0 0