首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > C/C++ > PKU2282 The Counting Problem
【标  题】:PKU2282 The Counting Problem
【关键字】:PKU2282,The,Counting,Problem
【来  源】:http://www.cppblog.com/sicheng/archive/2007/02/20/18882.html

PKU2282 The Counting Problem

看看你的心有多细?

The Counting Problem
Time Limit:3000MS? Memory Limit:65536K
Total Submit:741 Accepted:368

Description
Given two integers a and b, we write the numbers between a and b, inclusive, in a list. Your task is to calculate the number of occurrences of each digit. For example, if a = 1024 and b = 1032, the list will be

1024 1025 1026 1027 1028 1029 1030 1031 1032

there are ten 0's in the list, ten 1's, seven 2's, three 3's, and etc.

Input
The input consists of up to 500 lines. Each line contains two numbers a and b where 0 < a, b < 100000000. The input is terminated by a line `0 0', which is not considered as part of the input.

Output
For each pair of input, output a line containing ten numbers separated by single spaces. The first number is the number of occurrences of the digit 0, the second is the number of occurrences of the digit 1, etc.

Sample Input

1 10
44 497
346 542
1199 1748
1496 1403
1004 503
1714 190
1317 854
1976 494
1001 1960
0 0

Sample Output

1 2 1 1 1 1 1 1 1 1
85 185 185 185 190 96 96 96 95 93
40 40 40 93 136 82 40 40 40 40
115 666 215 215 214 205 205 154 105 106
16 113 19 20 114 20 20 19 19 16
107 105 100 101 101 197 200 200 200 200
413 1133 503 503 503 502 502 417 402 412
196 512 186 104 87 93 97 97 142 196
398 1375 398 398 405 499 499 495 488 471
294 1256 296 296 296 296 287 286 286 247

Source
Shanghai 2004

我采用的是每一位统计每一个数字的方法
我的想法就是 某一位出现某个数字的次数 就是其他位可能出现的数字的总和
比如1134 第二位出现1就应该是前面的1+后面的34+1(还有00呢) 故是135种
下面我列出了我的草稿:
(0代表是0的情况 <代表小于本位数字 =代表等于本位数字 >代表大于本位数字)
(post代表后面形成的数字 pre代表前面形成的数字)
第一位
0: 0
<:本位权
=:?? pre+1
>:? 0
第K位
0:??? pre*本位权
<:?? (pre+1)*本位权
=:?? pre*本位权+post+1
>:? pre*本位权
最后一位
0 || <= : pre+1
> :??????? pre
注意 如果数字只有1位 则不能应用第一位规则 而应该应用最后一位规则
我WA了一次这里

Solution
//by oyjpArt

?

?1#include?<stdio.h>
?2#include?<math.h>
?3#include?<memory.h>
?4
?5const?int?N?=?10;
?6int?w[N],?d[N],?num1[N],?num2[N],?nd;?//???¨,??×?,????????????1,????2,????
?7
?8inline?int?pre(int?pos)?{
?9????int?tot?=?0,?i,?base;
10????for(base?=?1,?i?=?pos-1;?i>=0;?i--)?{
11????????tot?+=?d[i]*base;
12????????base?*=?10;
13????}

14????return?tot;
15}

16
17inline?int?post(int?pos)?{
18????int?tot?=?0,?i,?base;
19????for(base?=?1,?i?=?nd-1;?i>pos;?i--)?{
20????????tot?+=?d[i]*base;
21????????base?*=?10;
22????}

23????return?tot;
24}

25
26void?cal(int?x,?int?num[])?{
27????int?base?=?1,?i,?j,?tmp?=?x;
28????nd?=?(int)ceil(log10(x+1));?//????????
29????if(nd?==?0)?++nd;
30????for(i?=?nd-1;?i>=0;?i--)?{?//?????????????¨?????·?????????????
31????????w[i]?=?base;
32????????base?*=?10;
33????????d[i]?=?tmp%10;
34????????tmp?/=?10;
35????}

36????for(i?=?0;?i<nd;?i++)?{?//??????i??
37????????if(i?==?0?&&?nd?!=?1)??//?????????????í?
38????????????for(j?=?0;?j<=9;?j++)?{?//??????×?j??i?????????????????
39????????????????if(j?!=?0?&&?j?<?d[i])????????num[j]?+=?w[i];?//±????¨
40????????????????else?if(j?==?d[i])????num[j]?+=?post(i)+1;?//??i+1????????????×?+1
41????????????}

42
43????????else?if(i?==?nd-1)??//×??ó???????????í
44????????????for(j?=?0;?j<=9;?j++)?{
45????????????????if(j?<=?d[i])???????num[j]?+=?pre(i)+1;?//i?°??????????×?+1
46????????????????else????????????????num[j]?+=?pre(i);
47????????????}

48
49????????else????????????//??°??é??
50????????????for(j?=?0;?j<=9;?j++)?{?
51????????????????if(j?==?0)?{
52????????????????????if(d[i]?==?0)???num[j]?+=?(pre(i)-1)*w[i]?+?post(i)+1;
53????????????????????else????????????num[j]?+=?pre(i)*w[i];
54????????????????}

55????????????????else?if(j?<?d[i])???num[j]?+=?(pre(i)+1)*w[i];
56????????????????else?if(j?==?d[i])??num[j]?+=?pre(i)*w[i]?+?post(i)+1;
57????????????????else????????????????num[j]?+=?pre(i)*w[i];
58????????????}

59????}

60}

61
62int?main()?{
63????int?a,?b,?t,?i;
64????while(scanf("%d%d",?&a,?&b),?a+b)?{
65????????memset(num1,?0,?sizeof(num1));
66????????memset(num2,?0,?sizeof(num2));
67????????if(a?>?b)?{
68????????????t?=?a;
69????????????a?=?b;
70????????????b?=?t;
71????????}

72????????if(a?>?0)?cal(a-1,?num1);
73????????cal(b,?num2);
74????????printf("%d",?num2[0]-num1[0]);
75????????for(i?=?1;?i<10;?i++)
76????????????printf("?%d",?num2[i]-num1[i]);
77????????putchar('\n');
78????}

79????return?0;
80}

81
这个注释不知道怎么拷出来就变成乱码了 请高手指点
我的毕设你帮忙:[问题更新]几个问题请有经验的高手进来看看~~春节快乐噢~:【上一篇】
ACE框架的三大部分:【下一篇】
【相关文章】
  • U-Soft ABIS meets fathers of Analysis Services
  • List sessions and some details about them
  • Retreive new hashvalue and display their load
  • A Crash Course on the Depths of Win32 Structured Exception Handling
  • 也谈博客园的赢利模式 ---回应Where is the money??? – Here
  • The best of Firefox Tricks, Extensions and Website
  • Why are the standard containers so slow?
  • Kickstarting Google Web Toolkit on the Client Side
  • 分享2本CG实时渲染书籍: ShaderX3 与 Physically Based Rendering : From Theory to Implementation 连接已...
  • The first step of Java[8]
  • 【随机文章】
  • 如何开发辅助决策系统?
  • 一组数据摘要算法的效率测试
  • C++流的概念
  • Windows 2000启动菜单详解
  • 多用户应用程序中应注意问题3
  • SQL Server 中易混淆的数据类型
  • iBatis对批量update的支持
  • QQ被盗这样找回来[鉴于好友的QQ被盗]
  • 《 V I M 教 程 》
  • OpenGL中的矩阵存储方式
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.