UVa1225Digit Counting(计算1-n的整数0-9各出现了多少次,神级代码...)
来源:互联网 发布:软件开发公司网站 编辑:程序博客网 时间:2024/06/04 18:30
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
#include<cstdio>int a[10000][10];int main(){ for(int i=1;i<10000;i++) { for(int j=0;j<10;j++) a[i][j]=a[i-1][j]; for(int k=i;k;k/=10) a[i][k%10]++; } int n; scanf("%d",&n); for(int i=0;i<n;i++) { int num; scanf("%d",&num); for(int j=0;j<9;j++) printf("%d ",a[num][j]); printf("%d\n",a[i][9]); } return 0;}
另外还有一份代码:
#include<stdio.h>#include<string.h>#include<ctype.h>#define max 40000+10char s[max];int main(){ int T; scanf("%d", &T); while (T--) { int n; int first = 1; scanf("%d", &n); char*p = s; for (int i = 1; i <= n; i++) { sprintf(p, "%d", i); if (i < 10) p++;//指针移动的位数要随着数字位数而调整 else if (i < 100) p += 2; else if (i < 1000) p += 3; else if (i < 10000) p += 4; else p += 5; } int a[10]; memset(a, 0, sizeof(a)); int len = strlen(s); for (int i = 0; i < len; i++) { a[s[i]-'0']++;//源代码网上看到的,觉得Ta用switch太啰嗦,遂把下面的注释不封改成了这句 /** switch (s[i]) { case'0': <span style="white-space:pre"></span>a[0]++; break; case'1': a[1]++; break; case'2': a[2]++; break; case'3': a[3]++; break; case'4': a[4]++; break; case'5': a[5]++; break; case'6': a[6]++; break; case'7': a[7]++; break; case'8': a[8]++; break; case'9': a[9]++; break; } */ } for (int i = 0; i < 10; i++) { if (first) //注意第一个字符处不要有空格 first = 0; else printf(" "); printf("%d", a[i]); } printf("\n"); } return 0;}
- UVa1225Digit Counting(计算1-n的整数0-9各出现了多少次,神级代码...)
- 计算在区间 1 到 n 的所有整数中,数字 x(0 ≤ x ≤ 9)共出现了多少次?
- 从0,1,2...n中统计0,1,2...9各出现了多少次【SWUN1597】
- 计算1~100的所有整数中出现多少次数字9
- 编写程序数一下1到100的所有整数中出现了多少次数字9
- 【C语言】计算1至100出现了多少次9
- 从1到N这N个数中1的出现了多少次?
- 数1-n个数中0-9出现过多少次
- 编写程序数一下1到100的所有整数中出现多少次数字9
- 1到100的所有整数中出现多少次数字9。
- 编写程序数1-100的所有整数中出现多少次数字9
- 编写程序数一下1到100的所有整数中出现多少次数字9。
- C语言实现“1到100的所有整数中出现多少次数字9”
- 编写程序数一下 1到 100 的所有整数中出现多少次数字 9
- 编写程序数一下1到100的所有整数中出现多少次数字9
- 1到 100 的所有整数中出现多少次数字 9
- 编写程序数一下1到100的所有整数中出现多少次数字9
- 编写一个程序,数一下1—100的所有整数中出现多少次数字9
- dns怎么做负载均衡
- 如何成为一名优秀的程序员? (一)
- 程序员之路
- 城市地图
- 程序员高薪之路
- UVa1225Digit Counting(计算1-n的整数0-9各出现了多少次,神级代码...)
- 如何进入程序设计的领域
- 从程序员到系统分析员(一)
- 优秀员工的十大特征
- GSON源码解析
- java.sql.SQLException: ORA-00904:标识符无效
- 把C语言讲的这么幽默也是厉害
- 学习Linux内核启动过程:从start_kernel到init
- 【RxJava Demo分析】(四)RxBus