您的位置:首页 > 其它

UVA 11038 - How Many O's? (数学题)

2013-09-24 10:55 239 查看
传送门:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=470&problem=1979

 

题意:求[m,n]中有多少个0

题解:分位数分析:

n的第i位不为0=n左边的数(高位)*10^(i-1)

n的第i位为0 =(n左边的数-1)*10^(i-1)+(i位右边的数+1)

ans=f(n)-f(m-1);

注意m为0的情况即可。

 

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
using namespace std;

#define si1(a) scanf("%d",&a)
#define si2(a,b) scanf("%d%d",&a,&b)
#define sd1(a) scanf("%lf",&a)
#define sd2(a,b) scanf("%lf%lf",&a,&b)
#define ss1(s)  scanf("%s",s)
#define pi1(a)    printf("%d\n",a)
#define pi2(a,b)  printf("%d %d\n",a,b)
#define mset(a,b)   memset(a,b,sizeof(a))
#define forb(i,a,b)   for(int i=a;i<b;i++)
#define ford(i,a,b)   for(int i=a;i<=b;i++)

typedef long long LL;
const int N=110001;
const int M=1000007;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
const double eps=1e-7;

LL xiaohao(LL l)
{
LL sum=0,j=1,r=0,m;
while(l)
{
m=l%10;
l/=10;
if(m)   sum+=l*j;
else    sum+=(l-1)*j+r+1;

r=r+m*j;
j=j*10;
}
return sum;
}

int main()
{
LL n,m;
while(scanf("%lld%lld",&m,&n)&&m>=0)
{
LL x=xiaohao(n)-xiaohao(m-1);
if(m==0)    x++;
printf("%lld\n",x);
}
return 0;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: