阿倍森

导航

关于用Python简单实现多项式加法和乘法

要保证指数的输入必须是呈递增趋势

import numpy as np

a = np.zeros((10000,1))#用矩阵来模拟一维数组
b = np.zeros((10000,1))
c = np.zeros((10000,1))
d = np.zeros((10000,1))
n = int(input("请输入多项式A的个数"))
for i in range(0,n):
print('%d:'%i)
x = int(input('系数'))
z = int(input('指数'))
a[z] = x
n = int(input("请输入多项式B的个数"))
for i in range(0,n):
print('%d:'%i)
x = int(input('系数'))
z = int(input('指数'))
b[z] = x
def multiplication(a,b,c)->None:
for i in range(0,100):
if a[i]:
for j in range(0,100):
if b[j]:
c[i+j] += a[i]*b[j]
for i in range(0,100):
if c[i]:
print(int(c[i]),"x^",i,end = " ")
def addition(a,b,d)->None:
for i in range(0,100):
if a[i]:
d[i]+=a[i]
if b[i]:
d[i]+=b[i]
for i in range(0,100):
if d[i]:
print(int(d[i]),"x^",i,end = " ")
print("加法:")
addition(a,b,d)

print("\n乘法:")
multiplication(a,b,c)

posted on 2022-01-24 16:56  阿倍森  阅读(264)  评论(0编辑  收藏  举报