POJ 3286How many 0's?
来源:互联网 发布:dnf优化防卡补丁 编辑:程序博客网 时间:2024/06/05 08:15
Description
A Benedict monk No.16 writes down the decimal representations of all natural numbers between and including m and n, m ≤ n. How many 0's will he write down?
Input
Input consists of a sequence of lines. Each line contains two unsigned 32-bit integers m and n, m ≤ n. The last line of input has the value of m negative and this line should not be processed.
Output
For each line of input print one line of output with one integer number giving the number of 0's written down by the monk.
Sample Input
10 11100 2000 5001234567890 23456789010 4294967295-1 -1
Sample Output
122929876543043825876150
Source
比如算4123中有多少个2
按位统计,,,先算各位,,个位是2的情况有413种,,,因为各位左边可以0~412,,,而右边没有数字,,,
然后是十位,,,十位是2的有41*10 + 1*4种,,当左边从0~40时,,,右边可以从0~9,,,而左边为41时,,右边只能从0~3
然后是百位,,,,百位有4*100种,,,,即左边从0~3,,右边从0~99
千位有 1*1000,,,左边没有数字,,,右边0~999,,,,
上面是计算1~9,,,,计算0的时候比较特殊,,,,原因是除了0这一个数字之外,,,,0不能做开头,,,
可以看到在求1~9的个数的时候,,,都是分为2部分相乘,,,这样0的处理也很简单,,只需把相乘的左半部分-1,,,,
#include<stdio.h>long long count[12]={1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000,10000000000,100000000000};long long find(long long x){ long long left,sum=0,w; for(int a=1;a<12;a++) { left=x/count[a]-1; sum+=left*count[a-1]; w=(x%count[a]-x%count[a-1])/count[a-1]; if(w>0)sum+=count[a-1]; else if(w==0)sum+=x%count[a-1]+1; if(x<count[a])break; } return sum;}int main(){ long long m,n; while(scanf("%lld%lld",&m,&n)&&n>=0) { printf("%lld\n",find(n)-find(m-1)); } return 0;}
- poj 3286 How many 0's?
- POJ 3286How many 0's?
- poj 3286 How many 0's?
- poj 3286 How many 0's?
- poj-3286-How many 0's?
- POJ 3286 How many 0's?
- POJ 3286 How many 0's?
- poj-3286 How many 0's?
- poj 3286 How Many 0's?
- POJ 3286 How many 0’s?
- poj 3286 How many 0's? (数位DP+ 统计)
- poj 3286 How many 0's? --- 数位dp
- How many 0's? - POJ 3286 数位dp
- poj 3286 How many 0's? 按位统计
- POJ 3286 How many 0's?(数论)
- POJ 3286 How many 0's?(数位DP)
- POJ 3286 How many 0's? (数位dp)
- POJ 3286 How many 0's?(数位dp)
- 专题6-3.函数调用行为, 递归函数(国嵌C语言视频)
- ajax+json+Struts2实现list传递
- RTSP 服务端和客户端
- Oracle 绑定变量 详解
- linux设备驱动篇之LED驱动(二)
- POJ 3286How many 0's?
- HTTP协议详解
- mysql5.5.29绿色版的安装
- sql将逗号分隔的字符串转为表Table
- uva难度表
- Oracle database 15章 恢复管理器 闪回恢复区 理论试验
- heike
- css选择器的优先级别和如何编写简洁、高效的css
- asp.net的<% %>总结