Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
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+10
- char 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;
- }
題目:輸出1~N所有數字中,0~9出現的總次數。
分析:簡單題。打表計算,查詢輸出即可。
說明:最近事情好多啊╮(╯▽╰)╭。
- #include <algorithm>
- #include <iostream>
- #include <cstdlib>
- #include <cstring>
- #include <cstdio>
- #include <cmath>
- using namespace std;
- int f[10000][10];
- int main()
- {
- memset(f, 0, sizeof(f));
- for (int i = 1 ; i < 10000 ; ++ i) {
- for (int j = 0 ; j < 10 ; ++ j)
- f[i][j] = f[i-1][j];
- int left = i;
- while (left) {
- f[i][left%10] ++;
- left /= 10;
- }
- }
- int t,n;
- while (~scanf("%d",&t))
- while (t --) {
- scanf("%d",&n);
- for (int i = 0 ; i < 9 ; ++ i)
- printf("%d ",f[n][i]);
- printf("%d\n",f[n][9]);
- }
- return 0;
- }
數字轉字符串常用 %10;/=10配套。
while(~scanf("%d",&n))
<=> while(scanf("%d",&n)!=EOF)
1.正常輸入的時候,scanf返回輸入的數字如1,2,3等等,對這些數字取非,不會成爲0,就會執行循環;
2.錯誤輸入指的就是沒有輸入的時候,scanf返回的是EOF(End Of File),EOF=-1,對EOF取非,就是對-1取非
[~是位運算,它是將數據在內存中的每一位(當然是二進制)取反。-1在內存中所有位全部爲1,~(-1)=0,即對-1取非就是0]
就會跳出循環。for (i = 0; a[i]; i++)
a[i]是個判斷,在這裏相當於if(a[i]),而if(a[i])也就是 a[i]!=0,在ASCII碼中0代表的是'\0',所以整個這句話相當於 for (i = 0; a[i]!='\0'; i++) '\0'在C語言裏是判斷字符串結束的標誌符。
原文鏈接:http://blog.csdn.net/mobius_strip/article/details/44346607
http://blog.csdn.net/hurmishine/article/details/50880092