UVa 1225 - Digit Counting
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
相關文章
- Digit Counting uva1225Git
- 生成元(Digit Generator, AMC/ICPC Seoul 2005, UVa1583)Git
- GCD CountingGC
- Nth Digit【難】Git
- Last digit of a huge numberASTGit
- ZOJ First Digit(瞎搞)Git
- 計數排序 - Counting Sort排序
- D - Digit vs Square RootGit
- Automatic Reference Counting-SwiftSwift
- LintCode-Digit CountsGit
- solution-uva1594
- python ref counting based garbage collectionPython
- iOS中的Reference Counting詳解iOS
- 【LeetCode】Counting Bits(338)LeetCode
- LeetCode 338 Counting BitsLeetCode
- Codeforces 954H Path Counting
- Nth Digit 第N個數字Git
- LeetCode-Number of Digit OneLeetCodeGit
- 233. Number of Digit OneGit
- Leetcode 400.Nth DigitLeetCodeGit
- HDU 1060 Leftmost DigitGit
- Uva232 Crossword AnswersROS
- Uva 1590 IP Networks
- UVA 1593(Alignment of Code)
- UVA11624-Fire!
- UVA557 Burger 題解
- projecteuler---->problem=19----Counting SundaysProject
- CF1919E Counting Prefixes
- Uva 10410 Tree ReconstructionStruct
- UVA - 11178 Morley's TheoremREM
- UVa340 - Master-Mind HintsAST
- leetcode [python] 【338】Counting BitsLeetCodePython
- LeetCode 第 338 題 (Counting Bits)LeetCode
- Python演算法:Counting 101Python演算法
- 338. Counting Bits--LeetCode RecordLeetCode
- uva11292-Dragon of LoowaterGo
- uva10935卡片遊戲遊戲
- 計數排序+uva11462排序