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
Hint
The answer maybe very large, we recommend you to use long long instead of int.
分析:
发现有近似num / 10的规律,仔细观察后发现其实确实粗略来看是每10个出现一个和为10的倍数
因为对于一个数,例如1X,这时候个位只有10 - 1 = 9才满足,对于一个三位数例如12X,则由于1和2是确定的,所以在120 - 129这十个数的范围内(也就是个位0 - 9)只有10 - 1 - 2 = 7,即127满足,对于大一点的数,例如637859X,6 + 3 + 7 + 8 + 5 + 9 = 38,因为最后一位肯定只能为正(不可能为-8),所以只有最后一位为40 - 38 = 2,即6378592符合条件,所有的数都这么看,会发现每次高位确定了,其实在个位上只有0 - 9当中的一个数能够满足,这就印证了num / 10的规律
最后只要注意下右边界就行了,即
0 - 90 -> 90 / 10 = 9个
但是
0 - 91 ->90 / 10 + 1 = 10个(由于91这个边界多加了一个)
处理方法是从90 / 10 * 10 = 90 -->91暴力扫一下,看有没有多出来一个能够满足各位和是10的倍数的,发现91是,所以就在9的基础上加1,因为我们证明了个位0 - 9有且只有一个数满足,所以只要扫过去发现一个直接返回就行了
注意:0可以被10整除!!
import java.util.*; public class Main{ static Scanner in = new Scanner(System.in); static long get(long num) { long n = num / 10 * 10; boolean flag = false; for (; n <= num; n++) { long tn = n, sum = 0; while (tn!=0) { sum += tn % 10; tn /= 10; } if (sum % 10 == 0) { flag = true; break; } } if (flag) return 1; else return 0; } static long solve(long num) { if (num < 0) return 0; if (num < 10) return 1; return num / 10 + get(num); } public static void main(String args[]){ int k = in.nextInt(); int cas = 0; while(k-->0) { cas++; long a = in.nextInt(); long b = in.nextInt(); long cnt = solve(b) - solve(a - 1);//-1就是为了把0算进去 System.out.println("Case #"+cas+": "+cnt); } } }