思路很简单,直接上代码
from sys import stdin
input = stdin.readline
t = int(input())
def sumdigit(x):
return sum(int(i) for i in str(x))
def f(ltrue,rtrue,temp):
tempmax = 0
if rtrue-ltrue <= 20:
for i in range(ltrue,rtrue+1):
tempmax = max(tempmax,sumdigit(i))
print(tempmax+temp)
return
else:
if len(str(ltrue))<len(str(rtrue)):
print(max(9*(len(str(rtrue))-1)+int(str(rtrue)[0])-1,sumdigit(str(rtrue)))+temp)
return
else:
if str(rtrue)[0] != str(ltrue)[0]:
print(max(9 * (len(str(rtrue)) - 1) + int(str(rtrue)[0]) - 1, sumdigit(str(rtrue)))+temp)
return
else:
f(int(str(ltrue)[1:]),int(str(rtrue)[1:]),temp+int(str(ltrue)[0]))
for _ in range(t):
l1,r1,l2,r2 = map(int,input().split())
ltrue = l1+l2
rtrue = r1+r2
f(ltrue,rtrue,0)