UVa 1225 - Digit Counting

UVA - 1225


Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

Status

Description

Download as PDF

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

题意:
把前n(n<=10000)个整数顺次写在一起,如n=15时,123456789101112131415
计算0-9各出现了多少次(输出10个数,分别是数字0-9出现的次数)

我不说话,就静静的看代码...

AC代码:
  1. #include<cstdio>  
  2. int a[10000][10];  
  3. int main()  
  4. {  
  5.     for(int i=1;i<10000;i++)  
  6.     {  
  7.         for(int j=0;j<10;j++)  
  8.             a[i][j]=a[i-1][j];  
  9.         for(int k=i;k;k/=10)  
  10.             a[i][k%10]++;  
  11.     }  
  12.     int n;  
  13.     scanf("%d",&n);  
  14.     for(int i=0;i<n;i++)  
  15.     {  
  16.         int num;  
  17.         scanf("%d",&num);  
  18.         for(int j=0;j<9;j++)  
  19.             printf("%d ",a[num][j]);  
  20.         printf("%d\n",a[i][9]);  
  21.     }  
  22.     return 0;  
  23. }  

另外还有一份代码:
  1. #include<stdio.h>  
  2. #include<string.h>  
  3. #include<ctype.h>  
  4. #define max 40000+10  
  5. char s[max];  
  6. int main()  
  7. {  
  8.     int T;  
  9.     scanf("%d", &T);  
  10.     while (T--)  
  11.     {  
  12.         int n;  
  13.         int first = 1;  
  14.         scanf("%d", &n);  
  15.         char*p = s;  
  16.         for (int i = 1; i <= n; i++)  
  17.         {  
  18.             sprintf(p, "%d", i);  
  19.             if (i < 10) p++;//指针移动的位数要随着数字位数而调整  
  20.             else if (i < 100) p += 2;  
  21.             else if (i < 1000) p += 3;  
  22.             else if (i < 10000) p += 4;  
  23.             else p += 5;  
  24.         }  
  25.         int a[10];  
  26.         memset(a, 0, sizeof(a));  
  27.         int len = strlen(s);  
  28.         for (int i = 0; i < len; i++)  
  29.         {  
  30.             a[s[i]-'0']++;//源代码网上看到的,觉得Ta用switch太啰嗦,遂把下面的注释不封改成了这句  
  31.             /** 
  32.             switch (s[i]) 
  33.             { 
  34.             case'0': 
  35.           <span style="white-space:pre">  </span>a[0]++; 
  36.                 break; 
  37.             case'1': 
  38.                 a[1]++; 
  39.                 break; 
  40.             case'2': 
  41.                 a[2]++; 
  42.                 break; 
  43.             case'3': 
  44.                 a[3]++; 
  45.                 break; 
  46.             case'4': 
  47.                 a[4]++; 
  48.                 break; 
  49.             case'5': 
  50.                 a[5]++; 
  51.                 break; 
  52.             case'6': 
  53.                 a[6]++; 
  54.                 break; 
  55.             case'7': 
  56.                 a[7]++; 
  57.                 break; 
  58.             case'8': 
  59.                 a[8]++; 
  60.                 break; 
  61.             case'9': 
  62.                 a[9]++; 
  63.                 break; 
  64.             } 
  65.             */  
  66.         }  
  67.         for (int i = 0; i < 10; i++)  
  68.         {  
  69.             if (first)           //注意第一个字符处不要有空格  
  70.                 first = 0;  
  71.             else  
  72.                 printf(" ");  
  73.             printf("%d", a[i]);  
  74.         }  
  75.         printf("\n");  
  76.     }  
  77.     return 0;  
  78. }  


题目:输出1~N所有数字中,0~9出现的总次数。

分析:简单题。打表计算,查询输出即可。

说明:最近事情好多啊╮(╯▽╰)╭。

  1. #include <algorithm>  
  2. #include <iostream>  
  3. #include <cstdlib>  
  4. #include <cstring>  
  5. #include <cstdio>  
  6. #include <cmath>  
  7.   
  8. using namespace std;  
  9.   
  10. int f[10000][10];  
  11.   
  12. int main()  
  13. {  
  14.     memset(f, 0, sizeof(f));  
  15.     for (int i = 1 ; i < 10000 ; ++ i) {  
  16.         for (int j = 0 ; j < 10 ; ++ j)  
  17.             f[i][j] = f[i-1][j];  
  18.         int left = i;  
  19.         while (left) {  
  20.             f[i][left%10] ++;  
  21.             left /= 10;  
  22.         }  
  23.     }  
  24.       
  25.     int t,n;  
  26.     while (~scanf("%d",&t))  
  27.     while (t --) {  
  28.         scanf("%d",&n);  
  29.         for (int i = 0 ; i < 9 ; ++ i)  
  30.             printf("%d ",f[n][i]);  
  31.         printf("%d\n",f[n][9]);  
  32.     }  
  33.     return 0;  
  34. }  



心得:

数字转字符串常用 %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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章