数据结构 一元多项式的加法与乘法


# 一元多项式的加法与乘法 一维数组实现,python
def addDxs(a,b):
i = 0
j = 0
c = []
# for i in range(len(a)-1):
# for j in range(len(b)):
while i<len(a)-1 and j<len(b)-1 :
if a[i+1]==b[j+1]:
if a[i]==-b[j]:
i=i+2
j=j+2

else:
c.append(a[i]+b[j])
c.append(a[i+1])
i = i + 2
j = j + 2
elif a[i+1]>b[j+1]:
c.append(a[i])
c.append(a[i+1])
i = i + 2

else:
c.append(b[j])
c.append(b[j+1])
j = j + 2

while i<=len(a)-1:
c.append(a[i])
i=i+1

while j<=len(b)-1:
c.append(b[j])
j=j+1

return c

def muldxs(a,b):#指数递降
i = 0
j = 0
d = []
e = []
if len(a)>0 and len(b)>0:
# for i in range (0,len(a)-1,2):
# for j in range(0,len(b)-1,2):
while i<len(a)-1:
print("第", i, "外层循环,共", len(a)/2, "次循环")
d=[]
j = 0
while j<len(b)-1:
print("第", j, "内层循环,共", len(b)/2, "次循环")
d.append(a[i]*b[j])
d.append(a[i+1]+b[j+1])
j=j+2
e = addDxs(e,d)
# del d
i=i+2

else:
e.append(0)

return e


# num = int(input())
# a = input('input a int:')
# a = a.split(' ')
#
# num1 = int(input())
# b = input('input b int:')
# b = b.split(' ')
a=[3,4,-5,2,6,1,-2,0]
b=[5,20,-7,4,3,1]
c=[]
# a=[]
# b=[5,24,-21,8,9,5]

print(a,b)

print(addDxs(a,b))

print(muldxs(a,b))


















 

 

posted on 2019-07-16 10:38  yaqiong1112  阅读(506)  评论(0编辑  收藏  举报