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
题目类型:简单数学
算法分析:枚举1到N的所有的正整数,对于每一个正整数从个位计算每一位上的数字并自加到频率数组diti相应的数字的值上。注意在判断并不断计算各位上的数字时,10的倍数需要特殊判断,下面第一个代码是更好的实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
/************************************************** filename :i.cpp author :maksyuki created time :2017/12/2 9:28:05 last modified :2017/12/2 9:31:08 file location :C:\Users\abcd\Desktop\TheEternalPoet ***************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("in", "r", stdin) #define CFO freopen ("out", "w", stdout) #define CPPFF ifstream cin ("in") #define CPPFO ofstream cout ("out") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define DBT printf("time used: %.2lfs\n", (double) clock() / CLOCKS_PER_SEC) #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e5 + 6666; int cnt[16]; int main() { #ifdef LOCAL CFF; //CFO; #endif int t; scanf("%d", &t); while(t--) { int n; scanf("%d", &n); memset(cnt, 0, sizeof(cnt)); for(int i = 1; i <= n; i++) { int j = i; while(j) { cnt[j%10]++; j /= 10; } } for(int i = 0; i <= 9; i++) { if(!i) printf("%d", cnt[i]); else printf(" %d", cnt[i]); } puts(""); } return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#include <iostream> #include <fstream> #include <cstring> using namespace std; int main() { int diti[16], cases, N; //ifstream cin ("aaa.txt"); cin >> cases; while (cases--) { cin >> N; int i, j; memset (diti, 0, sizeof (diti)); for (j = 1; j <= N; j++) { i = j; while ((i % 10) || (i % 10 == 0 && i / 10)) { diti[i%10]++; i /= 10; } } for (i = 0; i < 9; i++) printf ("%d ", diti[i]); printf ("%d\n", diti[i]); } return 0; } |
- « 上一篇:uva678
- uva1339:下一篇 »