ZOJ 2392 The Counting Problem(模拟)
2013-10-02 21:37
344 查看
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1368
题目大意:计算从S到T中所有的数,其中0,1,2,3,4,5,6,7,8,9的个数,例如从1024到1032有1024 1025 1026 1027 1028 1029 1030 1031 1032,其中10个0,10个1,7个2,3个3等等
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
代码如下:
题目大意:计算从S到T中所有的数,其中0,1,2,3,4,5,6,7,8,9的个数,例如从1024到1032有1024 1025 1026 1027 1028 1029 1030 1031 1032,其中10个0,10个1,7个2,3个3等等
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
代码如下:
# include<iostream> # include<cstdio> # include<cstring> # include<algorithm> # include<vector> using namespace std; int s,t; int res[10],dig[20],len; void solve(int num) { num ++; len =0; int temp =1; while(1) { dig[++len] = num%10; num /= 10; if(!num) break; temp *= 10; } for(int i=1; i<10; i++) res[i] += (len-1)*temp/10; res[0] += (len-1)*temp/10-(temp-1)/9; for(int i=1; i<dig[len]; i++) { res[i] += temp; for(int j=0; j<10; j++) res[j] += (len-1)*temp/10; } temp /= 10; for(int i=len-1; i>=1; i--) { for(int j=0; j<dig[i]; j++) { for(int k=len; k>i; k--) res[dig[k]] += temp; res[j] += temp; for(int k=0; k<10; k++) res[k] += (i-1)*temp/10; } temp /= 10; } } int main() { while(scanf("%d%d",&s,&t)&& s &&t) { memset(res,0,sizeof(res)); if(s>t) swap(s,t); solve(s-1); for(int i=0; i<10; i++) res[i] = -res[i]; solve(t); for(int i=0; i<9; i++) printf("%d ",res[i]); printf("%d\n",res[9]); } return 0; }
相关文章推荐
- 使IRB语法高亮方法的办法
- JavaScript入门
- Unity3D--day03
- TCP 和 UDP 传输的深入浅析,MSN 和 QQ 文件传输速度解析
- HDU 3336 KMP_NEXT
- 第二章5
- cocos2d-x精灵的放大和缩小
- 推广方式杂记
- HashMap与Hashtable的区别
- android系统图标的使用
- ubuntu 解决“无法获得锁 /var/lib/dpkg/lock -open (11:资源暂时不可用)”的方法
- 第二章例2-7
- 斐波那契数列
- 查询windows 电脑主板支持最大内存
- poj 3419 Difference Is Beautiful (区间最长连续不重复数 dp+二分+RMQ)
- 前端开发中常用的一些工具
- KMP算法详解-- 转自Matrix67
- 第一篇帖子
- Socket心跳包机制
- 检查内存泄露