现在的位置: 首页 > 综合 > 正文

uva 11038 – How Many O’s?

2013年08月31日 ⁄ 综合 ⁄ 共 502字 ⁄ 字号 评论关闭

题意:求[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的情况即可。

#include<cstdio>
#include<iostream>
#include<math.h>
#include<string.h>
using namespace std;
typedef long long LL;

LL find(LL l)
{
   LL k=0,n=1,res=0,m,r=0;
   while(l)
   {
      m=l%10;
      l=l/10;
      if(m)  res+=l*n;
      else   res=res+n*(l-1)+r+1;
      r+=m*n;
      n*=10;
   }
   return res;
}
int main()
{
    LL n,m,res;
    while(scanf("%lld %lld",&m,&n)&&(m>=0))
    {
     // cout<<find(m-1)<<"    "<<find(n)<<endl;
      res=find(n)-find(m-1);
      if(m==0) res++;
      printf("%lld\n",res);
    }
    return 0;
}

抱歉!评论已关闭.