輸入兩個多項式,輸出相加、相減、相乘的結果。例

LinkList多項式
*請勿使用全域變數

輸入兩個多項式,輸出相加、相減、相乘的結果。例如:

img


輸入說明
輸入兩筆資料,分別代表兩個多項式。
每一筆輸入 n 個整數,第一個代表 n-1次方的係數,第 n 個代表 0 次方的係數。

輸出說明
輸出共三行
第一行 輸出兩個多項式相加的結果
第二行 輸出兩個多項式相減的結果
第三行 輸出兩個多項式相乘的結果

多項式運算的結果,輸出計算後從最高次方到0次方的結果。
每一項需輸出係數、x、x的次方數
若係數為0,不輸出該項
若係數為1,則不輸出係數,僅需輸出正負號
若係數為正數且次方不為最高次方,需輸出+號
x的次方數 > 1,輸出ax^k (a為係數,k為次方數)
x的次方數 = 1,輸出ax (a為係數)
x的次方數 = 0,輸出a (a為係數)
如果所有係數都為0,則該行輸出0 (參考範例測資7)


img


img

img

求解此題

我用python写的,大致如此可以还有bug没发现

exp1 = '2x^4+3x^3+x^2-1'
exp2 = 'x^5-x^3+4x^2-3x+2'

def coefficients(expression):
    exp = expression
    if exp[0]=='x': exp = '1'+exp
    if exp[-1]=='x': exp += '^1'
    exp = exp.replace(' ','')
    exp = exp.replace('*x','x')
    exp = exp.replace('+x','+1x')
    exp = exp.replace('-x','-1x')
    exp = exp.replace('x+','x^1+')
    exp = exp.replace('x-','x^1-')
    exp = exp.replace('-','+-')
    exp = exp.split('+')
    for i,n in enumerate(exp):
        if 'x' not in n: exp[i]+='x^0'
    exp = [[*map(int,e.split('x^'))] for e in exp]
    dic = {}
    for e in exp:
        dic[e[1]] = dic.get(e[1],0)+e[0]
    for i in range(max(dic.keys())):
        if i not in dic: dic[i]=0
    exp = sorted(dic.items(),key=lambda x:x[0])
    return [i[1] for i in exp]

def coeffic(exp1,exp2):
    coe1,coe2 = coefficients(exp1),coefficients(exp2)
    len1,len2 = len(coe1),len(coe2)
    if len1<len2: coe1 += [0]*(len2-len1)
    if len1>len2: coe2 += [0]*(len1-len2)
    return [coe1[::-1],coe2[::-1]]

def expression(coefficients):
    t = []
    for i,n in enumerate(coefficients):
        if n: t.append(f"{n}x^{len(coefficients)-i-1}")
    res = ' + '.join(t).replace('+ -','- ').replace('x^0','')
    res = res.replace('^1 ',' ').replace('-1x','-x').replace(' 1x',' x')
    return res[1:] if res[:2]=='1x' else res if res!='' else '0'

def add(exp1,exp2):
    t = coeffic(exp1,exp2)
    t = list(map(sum,zip(t[0],t[1])))
    return expression(t) if expression(t) else '0'

def sub(exp1,exp2):
    t = coeffic(exp1,exp2)
    t = list(map(lambda x:x[0]-x[1],zip(t[0],t[1])))
    return expression(t) if expression(t) else '0'
    
def mul(exp1,exp2):
    coe1,coe2 = coeffic(exp1,exp2)
    lst,dic = [],{}
    for i in range(len(coe1)):
        for j in range(len(coe2)):
            lst.append([coe1[i]*coe2[j],i+j])
    for i in lst:
        dic[i[1]] = dic.get(i[1],0)+i[0]
    res = expression(dic.values())
    return res if res else '0'

def expression1(coefficients):
    res = []
    for i,n in enumerate(coefficients):
        if n: res.append(f"{n}x^{len(coefficients)-i-1}")
    tmp = ' + '.join(res).replace('+ -','- ').replace('x^0','')
    tmp = tmp.replace('^1 ',' ').replace('-1x','-x')
    return tmp


lst1 = list(map(int,input('Input:\n').split()))
lst2 = list(map(int,input().split()))
exp1,exp2 = expression(lst1),expression(lst2)
print('Output:')
print(add(exp1,exp2))
print(sub(exp1,exp2))
print(mul(exp1,exp2))

=========================== RESTART: D:\test多项式加减法.py ==========================
Input:
0 0 0 0 0
1 2 3 4 5 6 7 8
Output:
x^7 + 2x^6 + 3x^5 + 4x^4 + 5x^3 + 6x^2 + 7x + 8
-x^7 - 2x^6 - 3x^5 - 4x^4 - 5x^3 - 6x^2 - 7x - 8
0

=========================== RESTART: D:\test多项式加减法.py ==========================
Input:
100 2 3 90 20 -123
5 9 -1 -2 0 0
Output:
105x^5 + 11x^4 + 2x^3 + 88x^2 + 20x - 123
95x^5 - 7x^4 + 4x^3 + 92x^2 + 20x - 123
500x^10 + 910x^9 - 67x^8 + 275x^7 + 903x^6 - 531x^5 - 1307x^4 + 83x^3 + 246x^2

======================= RESTART: D:\test多项式加减法.py ==============================
Input:
2 3 0 1 -1
1 0 -1 4 -3 2
Output:
x^5 + 2x^4 + 2x^3 + 4x^2 - 2x + 1
-x^5 + 2x^4 + 4x^3 - 4x^2 + 4x - 3
2x^9 + 3x^8 - 2x^7 + 6x^6 + 5x^5 - 6x^4 + 11x^3 - 7x^2 + 5x - 2

=================== RESTART: D:\test多项式加减法.py ==================================
Input:
3 -7 1 2
7 -1 4
Output:
3x^3 + 6
3x^3 - 14x^2 + 2x - 2
21x^5 - 52x^4 + 26x^3 - 15x^2 + 2x + 8