洛谷-P1554 梦中的统计
题目背景
Bessie 处于半梦半醒的状态。过了一会儿,她意识到她在数数,不能入睡。
题目描述
Bessie的大脑反应灵敏,仿佛真实地看到了她数过的一个又一个数。她开始注意每一个数码((0 ldots 9)):每一个数码在计数的过程中出现过多少次?
给出两个整数 (M) 和 (N) ((1 leq M leq N leq 2 imes 10^9) 以及 (N-M leq 5 imes 10^5)),求每一个数码出现了多少次。
输入格式
第 (1) 行: 两个用空格分开的整数 (M) 和 (N)。
输出格式
第 (1) 行: 十个用空格分开的整数,分别表示数码 (0 ldots 9) 在序列中出现的次数。
输入输出样例
输入 #1
129 137
输出 #1
1 10 2 9 1 1 1 1 0 1
C++代码
#include <iostream>
using namespace std;
int main() {
int m, n, ans[10]={0};
cin >> m >> n;
for (int i=m; i<=n; ++i) {
int t = i;
while (t != 0) {
++ans[t % 10];
t /= 10;
}
}
for (int i=0; i<10; ++i)
cout << ans[i] << ' ';
cout << endl;
return 0;
}