f(sixleaves) = sixleaves

重剑无锋 大巧不工

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  95 随笔 :: 0 文章 :: 7 评论 :: 0 Trackbacks
1.其实是dp题目。用建表方法避免了重复计算

 1 #include <stdio.h>
 2 const int maxn= 10008;
 3 int count[maxn][10] = {0};
 4 int main() {
 5     
 6     
 7     int n;
 8     char buf[maxn] = {0};
 9     
10     //计算每一位
11     for (int i = 1; i<= maxn; i++) {
12         
13         sprintf(buf,"%d", i);
14         for (int j = 0; buf[j]; j++) {
15             count[i][buf[j]-'0']++;
16         }
17         
18     }
19     
20     //建立数据,防止重复计算
21     for (int i = 2; i <= maxn; i++) {
22         
23         for (int j = 0; j < 10; j++) {
24             
25             count[i][j] += count[i - 1][j];
26             
27         }
28     }
29     
30     scanf("%d", &n);
31     while (n > 0) {
32         int e;
33         scanf("%d", &e);
34         
35         for (int i = 0; i < 10; i++) {
36             printf( i == 9? "%d\n" : "%d ", count[e][i]);
37         }
38         
39         n--;
40     }
41 }
by sixleaves
posted on 2015-02-05 19:53 swp 阅读(493) 评论(1)  编辑 收藏 引用 所属分类: algorithm

评论

# re: UVA 1225 2015-08-04 09:34 yfs123456
输入9999你的程序不会输出数据,而是等待输入  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理