UVA 1225 Digit Counting(统计数位出现的次数)


Digit Counting

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

SubmitStatus

Description

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出现的次数)

以前就写了代码,不过那是网上的代码,不是很懂,现在看到了一份容易理解的代码,就记录下来.

以前博客链接:http://blog.csdn.net/hurmishine/article/details/50880092

AC代码:

#include <iostream>
#include <cstring>
using namespace std;
int main()
{
    int a[15];
    int t,n;
    cin>>t;
    while(t--)
    {
        memset(a,0,sizeof(a));
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            int t=i;
            while(t)
            {
                int num=t%10;
                a[num]++;
                t/=10;
            }
        }
        for(int i=0;i<10;i++)
        {
            if(i)
                cout<<" ";
            cout<<a[i];
        }
        cout<<endl;
    }
    return 0;
}
时间: 2024-11-05 16:33:32

UVA 1225 Digit Counting(统计数位出现的次数)的相关文章

UVa 1225 Digit Counting --- 水题

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

紫书第三章练习题:UVA 1225 Digit Counting by 15邱盼威

来源:http://m.blog.csdn.net/article/details?id=70861055 Trung is bored with his mathematicshomeworks. He takes a piece of chalk and starts writing a sequence ofconsecutive integers starting with 1 to N (1 < N < 10000). After that, hecounts the number

UVa 1225 - Digit Counting - ACM/ICPC Danang 2007 解题报告

1.题目大意 把前n$n\le 10000$个整数顺次写在一起:12345678910111213……计算0~9各出现了多少次. 2.思路 第一想法是打表,然而觉得稍微有点暴力.不过暂时没有想到更好的办法了,写完看了一下其它人的思路好像也差不多是打表的思路. 3.应注意的问题 (1)首先是格式问题,我第一次提交的时候PE了,因为没有意识到空格也会有影响.最开始我的最后一段代码是: for(i=0;i<10;i++) printf("%d ",s[n][i]); printf(&q

UVa 1225 - Digit Counting

题目:输出1~N所有数字中,0~9出现的总次数. 分析:简单题.打表计算,查询输出即可. 说明:最近事情好多啊╮(╯▽╰)╭. #include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> #include <cmath> using namespace std; int f[10000][10]; in

(UVA)1225 --Digit Counting(数数字)

题目链接:http://vjudge.net/problem/UVA-1225 #include <iostream> #include <cstring> #include <cstdio> using namespace std; int f[10000][10]; int main() { memset(f, 0, sizeof(f)); for (int i = 1 ; i < 10000 ; ++ i) { for (int j = 0 ; j <

1225 - Digit Counting

Trung is bored with his mathematics homeworks. He takes a piece of chalk and starts writing a sequence of consecutive integers starting with 1 to N <tex2html_verbatim_mark>(1 < N < 10000) <tex2html_verbatim_mark>. After that, he counts t

UVa 1640 The Counting Problem (数位DP)

题目 题目大意 给出\(a\).\(b\), 统计\(a\)和\(b\)(包含\(a\)和\(b\))之间的整数中, 数字\(0, 1, 2, 3, 4, 5, 6, 7, 8, 9\)分别出现了多少次.\(1 ≤ a, b ≤ 10^8\).注意, \(a\)有可能大于\(b\). 题解 设\(f_d(n)\)表示\(0 \cdots n\)中数字\(d\)出现的个数, 则求的是\(f_d(a) - f_d(b - 1)\). 暴力显然是会\(TLE\)的, 我们可以分段来求.例如我们要求\(

UVa1587.Digit Counting

题目连接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=247&page=show_problem&problem=3666 13764622 1225 Digit Counting Accepted C++11 0.035 2014-06-18 07:44:02 1225 - Digit Counting Time limit: 3.000 seconds Tru

UVa 1225 / UVALive 3996 Digit Counting 数数字(字符统计)

Digit Counting Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Trung is bored with his mathematics homeworks. He takes a piece of chalk and starts writing a sequence of consecutive integers starting