首页 > 其他 > 详细

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

时间:2017-04-27 22:44:44      阅读:386      评论:0      收藏:0      [点我收藏+]

来源: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邱盼威

原文:http://www.cnblogs.com/tzcacm/p/6777305.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!