码迷,mamicode.com
首页 > 其他好文 > 详细

UVa 1225 Digit Counting --- 水题

时间:2016-04-03 18:58:50      阅读:480      评论:0      收藏:0      [点我收藏+]

标签:

 

  UVa 1225

  题目大意:把前n(n<=10000)个整数顺次写在一起,12345678910111213...,数一数0-9各出现多少字

  解题思路:用一个cnt数组记录0-9这10个数字出现的次数,先将cnt初始化为0,接着让i从1枚举到n,

       对每个i,处理以活的i的每一个位置上的数,并在相应的cnt下标上+1

       最后输出cnt数组即可

技术分享
/* UVa 1225 Digit Counting --- 水题 */
#include <cstdio>
#include <cstring>

int cnt[15];    //cnt[i]记录数字i出现的次数

//取得n的各个位上的数,并在相应的cnt+1
void fun(int n){
    if (n == 0){
        return;
    }
    ++cnt[n % 10];
    fun(n / 10);
}

int main()
{
    int t, n;
    scanf("%d", &t);
    while (t--){
        scanf("%d", &n);
        memset(cnt, 0, sizeof cnt);
        for (int i = 1; i <= n; ++i){
            fun(i);
        }
        for (int i = 0; i <= 9; ++i){
            printf(i == 9 ? "%d\n" : "%d ", cnt[i]);
        }
    }//while(t)

    return 0;
}
View Code

 

UVa 1225 Digit Counting --- 水题

标签:

原文地址:http://www.cnblogs.com/tommychok/p/5350297.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!