只能过前三个测试点,求调

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

ppppposty @ 2024-10-20 14:52:47

n=int(input())
a=list(map(int,input().split(" ")))
if n==0:
    print(a[0])
else:
    out = ""
    for i in range(n + 1):
        w = str(n - i)
        # 判断最高位的系数
        if i == 0:
            if a[i] == 1:
                out = out + "x^" + w
                # print("x^" + w, end="")
            if a[i] == -1:
                out = out + "-x^" + w
                # print("-x^" + w, end="")
            if a[i] != 0 and a[i] != 1 and a[i] != -1:
                out = out + str(a[i]) + "x^" + w
                # print(str(a[i]) + "x^" + w, end="")
        if i != 0 and i != n:
            if a[i] == 1:
                out = out + "+" + "x^" + w
                # print("+" + "x^" + w, end="")
            if a[i] == -1:
                out = out + "-" + "x^" + w
                # print("-" + "x^" + w, end="")
            if a[i] > 1:
                out = out + "+" + str(a[i]) + "x^" + w
                # print("+" + str(a[i]) + "x^" + w, end="")
            if a[i] < -1:
                out = out + str(a[i]) + "x^" + w
                # print(str(a[i]) + "x^" + w, end="")
        if i == n:
            if a[i] > 0:
                out = out + "+" + str(a[i])
            if a[i] < 0:
                out = out + str(a[i])
    out=out.replace("x^1","x")
    out=out.replace("x^0","")
    if out=="":
        out="0"
    if out[0]=="+":
        out=out.replace("+","")
    print(out)

|