标签:
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.
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 .
For each test case, write sequentially in one line the number of digit 0, 1,...9 separated by a space.
2
3
13
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 }
标签:
原文地址:http://www.cnblogs.com/cyb123456/p/5769229.html