UVa1225

Digit Counting

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

Submit Status Practice UVA 1225 uDebug

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 to N (1 < N < 10000) . After that, he counts the number of times each digit (0 to 9) appears in the sequence. For example, with N = 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

题意:

输入一个正整数N,你需要不间断地列出从一到N的所有正整数,统计列举结果中数字0~9分别出现的次数。

输入:

情况数T,之后T行每行一个正整数N(N < 10000)

输出:

依次输出数字0~9的出现次数。

分析:

简单模拟,for循环从1到N,每次判断当前的数是几位数并分离每个数位的数字即可。

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 using namespace std;
 5 const int MAX_N = 1e5 - 1;
 6 int N;
 7 int a[13];
 8 int main(){
 9     int T; cin >> T;
10     while(T--){
11         scanf("%d",&N);
12         memset(a,0,sizeof a);
13         //printf("%d %d %d %d\n",a,b,c,d);
14         for(int i = 1; i <= N ; i++){
15             if(i < 10){
16                 a[i]++;
17             }
18             else if(i >= 10 && i < 100){
19                 int a_ = i / 10;
20                 int b = (i - a_ * 10);
21                 a[a_]++; a[b]++;
22             }
23             else if(i >= 100 && i < 1000){
24                 int a_ = i / 100;
25                 int b = (i - a_ * 100) / 10;
26                 int c = (i - a_ * 100 - b * 10);
27                 a[a_]++; a[b]++; a[c]++;
28             }
29             else if(i >= 1000 && i < 10000){
30                 int a_ = i / 1000;
31                 int b = (i - a_ * 1000) / 100;
32                 int c = (i - a_ * 1000 - b * 100) / 10;
33                 int d = (i - a_ * 1000 - b * 100 - c * 10);
34                 a[a_]++; a[b]++; a[c]++; a[d]++;
35             }
36         }
37         for(int i = 0 ; i <= 9 ; i++)
38             printf("%d%c",a[i],i == 9 ? ‘\n‘ : ‘ ‘);
39     }
40     return 0;
41 }

时间: 2024-10-12 20:12:14

UVa1225的相关文章

UVA1225 UVALive3996 Digit Counting

Regionals 2007 >> Asia - Danang 问题链接:UVA1225 UVALive3996 Digit Counting.入门练习题,用C语言编写程序. 这个问题是数字出现次数统计问题,按照套路处理就可以了. 本程序的套路包括,用运算符%从整数中取出数字,输出格式控制. AC的C语言程序如下: /* UVA1225 UVALive3996 Digit Counting */ #include <stdio.h> #include <memory.h>

UVa-1225 Digit Counting(数数字)

对于一个大于1且小于10000的整数N,我们定义一个唯一与之相关联的序列.例如,若N为13,则该序列为12345678910111213.现要求对于一个输入值N,记录这个序列中每个数字出现了多少次. 输入格式:先是一个整数以表明要输入的整数个数,之后每行给出一个大于1且小于10000的整数. 输出格式:每行输出每个数字出现的个数.如果输入为13,那么输出为1 6 2 2 1 1 1 1 1 1. 具体见https://cn.vjudge.net/problem/uva-1225 思路:没有多加思

算法竞赛入门经典(第二版)3-3数数字UVA1225

#include <cstdio> #include <string.h> int main(){ int n; scanf("%d", &n); getchar(); while (n--){ char str[10000]; scanf("%s",str) ; int len=strlen(str); int num=0; for(int i=0;i<10;i++) { for(int j=0;j<len;j++) {

UVa1225 Digit Counting

#include <stdio.h>#include <string.h> int main(){    int T, N, i, j;    int a[10];    scanf("%d", &T);    while (T--)    {        memset(a, 0, sizeof(a));        scanf("%d", &N);        for (i = 1; i <= N; ++i)  

数数字(Digit Counting,ACM/ICPC Danang 2007,UVa1225)

#include<stdio.h>#include<stdlib.h>#include<string.h>int main(){ char s[10000]; int a0 = 0, a1 = 0, a2 = 0, a3 = 0, a4 = 0, a5 = 0, a6 = 0, a7 = 0, a8 = 0, a9 = 0; scanf("%s", s); for (int i = 0; i < strlen(s); i++) { if (s[

数数字

Digit Counting [UVA-1225] 这题也是参考书上的习题3-2,题目也不贴了.题比较简单,值得留意的地方就是数组下角标的对应关系.C++实现如下: 1 #include<iostream> 2 using namespace std; 3 int main() 4 { 5 int i; 6 int n; 7 int cases; 8 cin >> cases; 9 while (cases--) 10 { 11 int tmp[10] = { 0 }; 12 ci

(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 <

算法竞赛入门经典第二版第三章习题

写这个的原因是看到一位大神的习题答案总结,于是自己心血来潮也想写一个这个,目的主要是督促自己刷题吧,毕竟自己太弱了. 习题3-1 得分 UVa 1585 大致就是设置一个变量记录到当前为止的连续的O的数量,碰到X就变0,水题. #include<stdio.h> #include<ctype.h> #include<string.h> char s[90]; int main(void) { int length,n,sum,num; scanf("%d&qu

2019年2月做题记录

UVA10082 (字符串常量水题) UVA272 (字符串替换水题) UVA401 (回文串镜像串水题) UVA340 (模拟题) UVA1583 (打表水题) UVA1584 (暴力) UVA1585 (模拟) UVA1586 (数学) UVA1225 (打表水题) UVA455 (KMP算法) UVA232 (模拟+思维) UVA202 (除法高精度水题) UVA1587 (思维) UVA10340 (模拟,定序求交集) 原文地址:https://www.cnblogs.com/Aya-U