Jimmy writes down the decimal representations of all natural numbers between and including m and n, (m ≤ n). How many zeroes will he write down?
Input starts with an integer T (≤ 11000), denoting the number of test cases.
Each case contains two unsigned 32-bit integers m and n, (m ≤ n).
For each case, print the case number and the number of zeroes written down by Jimmy.
5
10 11
100 200
0 500
1234567890 2345678901
0 4294967295
Case 1: 1
Case 2: 22
Case 3: 92
Case 4: 987654304
Case 5: 3825876150
题目大意:找0的个数
分析:此题需要考虑前导0的问题,加深了我对前导0的运用,和之前的一篇类似
代码:
/*思路,flag表示是否有前导0
如果没有前导0
flag的值为0;反之有前导0,flag的值为1
00089也是五位数,但是有前导0,之前一直忽略掉这个问题
*/
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
typedef long long ll;
ll dp[30][30];
int dig[30];
ll dfs(int pos,ll status,int flag,int limit)
{
if(pos==-1)
{
if(flag==0)
return status;
else
return 1;
}
if(flag==0)
{
if(dp[pos][status]!=-1&&!limit)
return dp[pos][status];
}
int up=limit?dig[pos]:9;
ll ans=0;
for(int i=0;i<=up;i++)
{
if(flag==1)
{
if(i==0)
{
ans+=dfs(pos-1,0,1,limit&&i==up);
}
else
ans+=dfs(pos-1,0,0,limit&&i==up);
}
else
{
if(i==0)
ans+=dfs(pos-1,status+1,0,limit&&i==up);
else
ans+=dfs(pos-1,status,0,limit&&i==up);
}
}
if(!limit&&!flag)
dp[pos][status]=ans;
return ans;
}
ll solve(ll x)
{
int id=0;
while(x)
{
dig[id++]=x%10;
x/=10;
}
return dfs(id-1,0,1,1);
}
int main()
{
memset(dp,-1,sizeof(dp));
ll y;
cin>>y;
ll a,b;
ll biaozhi=1;
while(y--)
{
scanf("%lld%lld",&a,&b);
cout<<"Case "<<biaozhi<<":"<<" "<<solve(b)-solve(a-1)<<endl;
biaozhi++;
}
return 0;
}