QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#719029#885. Keep Calm And Carry OffqwerasdfWA 19ms10704kbPython31.4kb2024-11-06 22:07:322024-11-06 22:07:32

Judging History

你现在查看的是最新测评结果

  • [2024-11-06 22:07:32]
  • 评测
  • 测评结果:WA
  • 用时:19ms
  • 内存:10704kb
  • [2024-11-06 22:07:32]
  • 提交

answer


import sys
input = lambda:sys.stdin.readline().strip()

def sub(a, b):
    # a - b
    
    l = len(a)
    ans = [0 for _ in range(l)]

    for i in range(l-1, -1, -1):
        if a[i] >= b[i]:
            ans[i] = a[i] - b[i]
        else:
            ans[i] = a[i] - b[i] + 10
            a[i-1] -= 1
    
    return ans

def solve(a, b):
    # a + x | b - x

    l = len(a)
    idx = l

    for i in range(l-1, -1, -1):
        if a[i] + b[i] >= 10:
            idx = i - 1
    
    while idx >= 0 and a[i] + b[i] == 9:
        idx -= 1
    
    res = [0 for _ in range(l)]
    for i in range(idx):
        res[i] = a[i]
    if idx < l:
        res[idx] = a[idx] + 1
    for i in range(idx+1, l):
        res[i] = 0

    # print(res)
    # print(a)
    # print()

    
    return sub(res, a)

a = [*map(int, [*input()])]
b = [*map(int, [*input()])]

la = len(a)
lb = len(b)

l = max(len(a), len(b)) + 1
a = [0 for _ in range(l-la)] + a
b = [0 for _ in range(l-lb)] + b

# print(a)
# print(b)

ans1 = solve(a, b)
ans2 = solve(b, a)

# print(ans1)
# print(ans2)

res = ans1

for i in range(l):
    if ans1[i] == ans2[i]:
        continue
    if ans1[i] < ans2[i]:
        res = ans1
        break
    else:
        res = ans2
        break

res = res[::-1]
while len(res) and res[-1] == 0:
    res.pop()
if len(res) == 0:
    res.append(0)
res = res[::-1]

print(*res, sep='')

详细

Test #1:

score: 100
Accepted
time: 16ms
memory: 10616kb

input:

10
99

output:

1

result:

ok answer is '1'

Test #2:

score: 0
Accepted
time: 8ms
memory: 10636kb

input:

90
10

output:

10

result:

ok answer is '10'

Test #3:

score: 0
Accepted
time: 12ms
memory: 10704kb

input:

23425
487915

output:

12085

result:

ok answer is '12085'

Test #4:

score: 0
Accepted
time: 13ms
memory: 10608kb

input:

1
1

output:

0

result:

ok answer is '0'

Test #5:

score: 0
Accepted
time: 19ms
memory: 10528kb

input:

99
99

output:

1

result:

ok answer is '1'

Test #6:

score: -100
Wrong Answer
time: 15ms
memory: 10524kb

input:

14
86

output:

4

result:

wrong answer expected '14', found '4'