Problem Description
If we sum up every digit of a number and the result can be exactly divided by 10, we say this number is a good number.
You are required to count the number of good numbers in the range from A to B, inclusive.
You are required to count the number of good numbers in the range from A to B, inclusive.
Input
The first line has a number T (T <= 10000) , indicating the number of test cases.
Each test case comes with a single line with two numbers A and B (0 <= A <= B <= 10 18).
Each test case comes with a single line with two numbers A and B (0 <= A <= B <= 10 18).
Output
For test case X, output "Case #X: " first, then output the number of good numbers in a single line.
Sample Input
2 1 10 1 20
Sample Output
Case #1: 0 Case #2: 1
找规律的题目!
你发现每10个数有一个符合条件的
但是你直接从A,B范围中找是不容易的
因为有个0
零也符合条件,如果直接找思路有点乱的话可能给你弄的内分泌失调!
那么怎么弄?
给出A,B找出0-A有几个符合条件,找出0-B有几个符合条件
两个一减就OK了!
下面上代码
!注意,所有的都要用long long !不用longlong 就是WA!
#include<stdio.h>
int judge(long long a)
{
int flag=0,l,k=0;
while(a>0)
{
k+=a%10;
a=a/10;
}
if(k%10==0)
{
flag=1;
}
return flag;
}
int main()
{
int t,step=0;
scanf("%d",&t);
while(t--)
{
step++;
long long a,b,num,sum,m,i,j;
scanf("%lld%lld",&a,&b);
num=0,sum=0,m=0,a--;
num=a/10;
for(j=a/10*10;j<=a;j++)
{
if(judge(j))
{
num++;
}
}
sum=b/10;
for(i=b/10*10;i<=b;i++)
{
if(judge(i))
{
sum++;
}
}
//printf("%lld %lld\n",num,sum);
printf("Case #%d: %lld\n",step,sum-num);
}
}