How many 0

How many 0

时间: 10ms        内存:128M

描述:

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 consists of a sequence of lines. Each line contains two unsigned 32-bit integers m and n, mn. The last line of input has the value of m negative and this line should not be processed.

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.

 

输入:

输出:

示例输入:

10 11
100 200
0 500
1234567890 2345678901
0 4294967295
-1 -1

示例输出:

1
22
92
987654304
3825876150

提示:

参考答案(内存最优[764]):

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <assert.h>

unsigned countl(char *s){
   unsigned r;
   if (strlen(s) == 1) return 1;
   if (s[0] == '0') {
      sscanf(s+1,"%lu",&r);
      return 1 + r + countl(s+1);
   }
   r = pow(10,strlen(s+1));
   r += (s[0]-'0') * strlen(s+1) * pow(10,strlen(s+1)-1);
   r += countl(s+1);
   return r;
}

unsigned count(char *s){
   unsigned r = 1; char tmp[20];
   if (strlen(s) == 1) return 1;
   for (r=0;r<strlen(s)-1;r++) tmp[r] = '9';
   tmp[r] = 0;
   r = count(tmp);
   r += (s[0]-'1') * strlen(s+1) * pow(10,strlen(s+1)-1);
   r += countl(s+1);
   return r;
}

char x[100], y[100];
unsigned u;

int main(){
   while (2 == scanf("%s%s",x,y) && x[0] != '-') {
      if (x[0] == '0') printf("%lu\n",count(y));
      else {
         sscanf(x,"%lu",&u);
         sprintf(x,"%lu",u-1);
         printf("%lu\n",count(y)-count(x));
      }
   }
	 return 0;
}

参考答案(时间最优[100]):

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
long long dp[10][10];
long long pow(long long a,long long b)
{
    long long ret=1;
    for(long long i=1; i<=b; i++)
        ret*=a;
    return ret;
}
long long solve(long long n,long long pos)
{
    long long s[15],len = 0,i,tem;
    while(n)
    {
        s[++len] = n%10;
        n/=10;
    }
    s[0] = 0;
    long long ans = 0,cnt = 0;
    for(i = len; i>=1; i--)
    {
        if(pos == 0 && i == len)
        {
            ans+=dp[i-1][pos]*(s[i]-1);
            tem = pow(10,i-1)-1;
            ans+=solve(tem,0);
        }
        else
            ans+=dp[i-1][pos]*s[i];
        ans+=cnt*pow(10,i-1)*s[i];
        if(pos)
        {
            if(s[i]>pos)
                ans+=pow(10,i-1);
            if(s[i]==pos)
                cnt++;
        }
        else if(i!=len)
        {
            if(s[i]>pos)
                ans+=pow(10,i-1);
            if(s[i]==pos)
                cnt++;
        }
    }
    return ans+cnt;
}

int main()
{
    memset(dp,0,sizeof(dp));
    for(long long i=1; i<=9; i++)
    {
        for(long long j=0; j<=9; j++)
            dp[i][j]=dp[i-1][j]*10+pow(10,i-1);
    }
    long long a,b;
    while(scanf("%lld %lld",&a,&b),a+b>=0)
    {
        long long flag=0;
        if(!a)flag = 1;
        if(a>b) swap(a,b);
        printf("%lld\n",solve(b,0)-solve(a-1,0)+flag);
    }
    return 0;
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注