G - Good Numbers

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.
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 <= 1018).
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

HintThe answer maybe very large, we recommend you to use long long instead of int.

如果我们把一个数字的每一个数字加起来,结果正好可以被10除,我们就说这个数字是个好数字。

你需要计算从A到B(含)范围内的好数字。

输入

第一行有一个数字T(T<=10000),表示测试用例的数量。

每个测试用例都有一行带有两个数字a和B(0<=a<=B<=1018)。

输出

对于测试用例X,首先输出“case#X:”,然后在一行中输出正确的数字。

样本输入

2

1 10

1 20

样本输出

Case #1: 0

Case #2: 1

提示

答案可能很大,我们建议您使用long long而不是int。

解题思路
在这里插入图片描述

#include <stdio.h>
 
int pd(long long a)   //判断是否是好数 
{
    long long k=0;
    while(a!=0){
        k=k+a%10;
        a=a/10;
    }
    if(k%10==0){
    	return 1; 
	}
    else{
    	return 0;
	}
}
 
int main()
{
    int T,i;
    long long res,a,b,sum,t;
    scanf("%d",&T);
    for(i=1;i<=T;i++)
    {
        scanf("%lld %lld",&a,&b);
        while(pd(a)==0){   //从a到b找出第一个好数 
        	a++;
		}
        while(pd(b)==0){   //从a到b找出最后一个好数 
        	b--;
		}
        if(a>b){    //如果a大于b,则没有好数 
        	res=0;
		}
        else{    //如果a<=b则有好数 
        	res=b/10-a/10+1;   //res为好数个数 
		} 
        printf("Case #%d: %lld\n",i,res);
    }
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值