不知道为什么第二个第五个点wrong了
查看原帖
不知道为什么第二个第五个点wrong了
574739
Aoung楼主2021/12/24 22:12
# 基本思路 先将int型当成一个整型计算 然后判断有几位小数 输出时将小数加上去即可
# a为底数 x为指数
# 非递归快速幂
#没有特判一开始即是小数的情况
def quick(a,n):#求a的n次方
    ans, res = 1,a
    while n:
        if n & 1:
            ans = ans * res
        res = (res * res)
        n >>= 1
    return ans


def judge():
    global s
    if len(num) == 1:  # 说明当前输入的数是一个整数 之间计算输出结果即可
        print(str(quick(int(num[0]), int(temp[1]))))
    else:  # 反之为一个浮点数 需要特殊处理
        num[1] = num[1].rstrip('0')
        x = int(num[0]) * (10 ** len(num[1])) + int(num[1])
        ans = str(quick(x, temp[1]))
        l = len(ans) - len(num[1]) * temp[1]  # 计算出结果后非小数部分的长度
        if l > 0:
            print(ans[:l] + "." + ans[l:].rstrip('0'))
        else:#特判 若l 为负数 则因加l的绝对值个0 到小数点后
            print('.' + '0' * abs(l) + ans[:].rstrip('0'))


while True:
    try:
        temp = input().split()
        temp[1] = int(temp[1])
        num = list(temp[0].split("."))
        judge()
    except:
        break
2021/12/24 22:12
加载中...