15行代码AC——习题3-3 数数字 (UVa1225,Digit Counting)
生活随笔
收集整理的这篇文章主要介绍了
15行代码AC——习题3-3 数数字 (UVa1225,Digit Counting)
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
大意:
把n(n<=10000)个整数顺序写在一起,求0~9分别出现多少次
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
分析:
水题不要想得太复杂,用最简单的知识就可以AC。复杂的语法反到易错而臃肿。
使用char[]存储数字列,用#include<algorithm>中的count()函数计算每个字符出现的次数。
传送门→懒癌的福音——algorithm头文件函数全集
提交链接→UVa-1225
代码:
#include <bits/stdc++.h> //万能头文件 using namespace std; int main() {int n; cin >> n; while(n--) {stringstream ss ; //实现整型、字符型的相互转化int num; cin >> num; char a[40000] ;for(int i = 1; i <= num; i++) ss << i;ss >> a ; int len = strlen(a) ; //字符数组长度, for(char i = '0'; i <= '9'; i++) cout << count(a,a+len,i) << (i=='9' ? "\n" : " "); //字符0-9在数组中出现的次数 }return 0 ; }择苦而安,择做而乐。虚拟现实终究比不过真实精彩之万一。
总结
以上是生活随笔为你收集整理的15行代码AC——习题3-3 数数字 (UVa1225,Digit Counting)的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 神奇的sstream头文件(整型与字符串
- 下一篇: 13行代码AC_习题3-9 子序列 (U