How many 0's?
来源:互联网 发布:单片机设计作品 编辑:程序博客网 时间:2024/06/08 03:29
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
// Problem#: 1370// Submission#: 2196597// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen University#include<stdio.h>#include<iostream>using namespace std;long long get_ans(long long u){ long long tmp=1,ans=1,k=u/10; while (k) { ans+=(k-1)*tmp; if (u-tmp*k*10+1>=tmp) ans+=tmp; else ans+=u-tmp*k*10+1; tmp*=10; k/=10; } return ans;}// 截断,先算出来从1----n 1----m-1 之后就可以知道一共有多少个0了 int main(){ long long ans,m,n; while (1) { cin>>m>>n; if (m<0) break; ans=get_ans(n); if (m>0) ans-=get_ans(m-1); cout<<ans<<endl; } return 0;}
#include <iostream>using namespace std;long long pow10[]={1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};long long num0(long long n){ long long i; long long cnt=1+n/10; if(n<0) return 0; for(i=1; pow10[i]<=n/10; i++) { long long left=(n/10)/pow10[i]; long long mid =( n/pow10[i] )%10; long long rit =n%pow10[i]; if(mid==0) cnt+=(left-1)*pow10[i] + rit+1; else cnt+=left*pow10[i]; } return cnt;}int main(){ long long m, n; while(cin>>m>>n && m>=0){ cout<<num0(n)-num0(m-1) <<endl; } return 0;}
- How many 0's?
- POJ3286 How many 0's?
- poj3286 How many 0's?
- poj 3286 How many 0's?
- poj 3405 How many 0's?
- POJ 3286How many 0's?
- poj 3286 How many 0's?
- POJ3286:How many 0's?(数位DP)
- poj 3286 How many 0's?
- 数位dp poj3286 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?
- UVa11038 How Many O's?
- sicily 1370 How many 0's? 递推 规律
- Linux系统下查看目录大小
- awk基本用法
- 话说程序猿好悲剧
- 使用HTML5和CSS3创建一个时髦的联系表单
- 虚函数
- How many 0's?
- cocos2d-x如何快速屏蔽触摸
- 基于 Android NDK 的学习之旅-----环境搭建
- 基于bootstrap + velocity的分页实现
- HTTP协议:HTTP请求消息/请求方法、HTTP响应消息/响应状态代码
- 改变linux终端颜色
- IPhone开发工具篇-Smart svn的使用一
- NET程序员讨论群
- 原味google