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

Posted 台州学院ACM集训队

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了紫书第三章练习题: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 of times each digit (0 to 9) appears in the sequence. Forexample, with N = 13, the sequence is: 12345678910111213

In this sequence, 0appears once, 1 appears 6 times, 2 appears 2 times, 3 appears 3 times, and eachdigit from 4 to 9 appears once. After playing for a while, Trung gets boredagain. He now wants to write a program to do this for him. Your task is to helphim with writing this program. Input The input file consists of several datasets. The first line of the input file contains the number of data sets whichis a positive integer and is not bigger than 20. The following lines describethe data sets. For each test case, there is one single line containing thenumber N. Output For each test case, write sequentially in one line the numberof digit 0, 1, . . . 9 separated by a space.

Sample 

Input 

13 

Sample 

Output 

0 1 1 1 0 0 0 0 0 0

1 6 2 2 1 1 1 1 1 1

链接:https://cn.vjudge.net/contest/160964#problem/A

题意:输入一个N, 统计1~N中,0~9出现的次数;

解题:

因为N最大只有10000,组数<=20,可以直接循环统计0~9 的个数。

代码如下:

 1 #include<stdio.h>
 2 int main()
 3 {
 4     int t,n;
 5     scanf("%d",&t);
 6     while(t--)
 7     {
 8         int a[10]={0},i;
 9         scanf("%d",&n);
10         for(i=1;i<=n;i++)
11         {
12             int x=i;
13             while(x)
14             {
15                 a[x%10]++;
16                 x/=10;
17             }
18         }
19         for(i=0;i<9;i++)
20         printf("%d ",a[i]);
21         printf("%d\n",a[9]);
22     }
23 }

 

以上是关于紫书第三章练习题:UVA 1225 Digit Counting by 15邱盼威的主要内容,如果未能解决你的问题,请参考以下文章

Digit Counting UVA - 1225

UVa 1225 Digit Counting --- 水题

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

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

UVA-1225 Digit Counting

UVA 1225 Digit Counting