Loading

高途笔试3

import sys

line = (sys.stdin.readline().strip())
nums = line.split()
num1, num2 = nums[0], nums[1]



def product(bit: str, x: str):
    ans = ''
    c = 0
    b = int(bit)
    for i in range(len(x) - 1, -1, -1):
        tmp = int(x[i]) * b + c
        ans = str(tmp % 10) + ans
        c = tmp // 10
    if c:ans  = str(c)+ans
    return ans


def add(x: str, y: str):
    if len(x) > len(y):x, y = y, x
    i, j = len(x) - 1, len(y) - 1
    c = 0
    ans = ''
    while i >= 0 and j >= 0:
        tmp = int(x[i]) + int(y[j]) + c
        ans = str(tmp % 10)+ans
        c = tmp // 10
        i, j = i - 1, j - 1
    while j >= 0:
        tmp = int(y[j]) + c
        ans = str(tmp % 10)+ans
        c = tmp // 10
        j = j - 1
    if c: ans = str(c)+ans
    return ans


def solve(num1, num2):
#     print(num1, num2)
    if num1 == '0' or num2 == '0': return '0'
    ans = '0'
    i = 0
#     print(len(num1))
    for j in range(len(num2) - 1, -1, -1):
        tmp = product(num2[j], num1)
        tmp += '0' * i
#         print(ans, tmp)
        i += 1
        ans = add(ans, tmp)
    return ans

ans = solve(num1, num2)
print(ans)

 

posted @ 2022-09-02 21:39  ArkiWang  阅读(94)  评论(0编辑  收藏  举报