??? 为什么样例全对,只有20分,求大佬帮帮忙(大哭)

P1067 [NOIP2009 普及组] 多项式输出

LG233666 @ 2024-03-21 22:07:06


n = int(input())
li = list(map(int, input().split()))   # 列表里面有n+1个数字
s = ''
for i in range(1, n + 2):
    if li[i-1] == 0:
        continue

    if n - i + 1 == 0:  # 多项式末位,指数为0
        s += '+' + str(li[i-1])
        continue

    if n - i + 1 == 1:  # 多项式倒数第二位,指数为1
        if li[i-1] > 0:
            s += '+' + str(li[i-1]) + 'x'
        else:
            s += str(li[i-1]) + 'x'
        continue

    if i == 1:
        if li[i-1] > 0:
            if li[i-1] == '1':    # 不是字符串'-1'
                s += 'x^%d' % n
            else:
                s += str(li[0]) + 'x^%d' % n
            continue

    if li[i-1] > 0:
        if li[i - 1] == 1:
            s += '+' + 'x^%d' % (n - i + 1)
        else:
            s += '+' + str(li[i-1]) + 'x^%d' % (n - i + 1)
    else:
        if li[i - 1] == -1:
            s += '-x^%d' % (n - i + 1)

        else:
            s += str(li[i-1]) + 'x^%d' % (n - i + 1)

print(s)

|