Python 链表多项式减法计算
我正在尝试做多项式减法的算法。 在我的Jupiter笔记本上,输出没有问题,但是程序评分系统给出了一个输出错误。 所以,我想知道在我的算法中是否存在错误答案或错误输出的可能性Python 链表多项式减法计算,python,singly-linked-list,polynomials,Python,Singly Linked List,Polynomials,我正在尝试做多项式减法的算法。 在我的Jupiter笔记本上,输出没有问题,但是程序评分系统给出了一个输出错误。 所以,我想知道在我的算法中是否存在错误答案或错误输出的可能性 class getNode: def __init__(self,coef=None, exp=None, link=None): self.head = None self.coef = coef self.exp = exp self.next
class getNode:
def __init__(self,coef=None, exp=None, link=None):
self.head = None
self.coef = coef
self.exp = exp
self.next = None
self.size = 0
def append (self, coef, exp, link=None):
if self.size == 0:
self.head = getNode(coef, exp, link)
self.coef = coef
self.exp = exp
self.next = link
self.size += 1
else:
cur = self.head
while cur.next != None:
cur = cur.next
cur.next = getNode(coef, exp, link)
self.coef = coef
self.exp = exp
self.next = link
self.size += 1
def print_nd(self):
print(" ", end='')
cur = self.head
while cur.next != None:
if cur.exp != 0:
print(cur.coef, cur.exp, end=' ')
cur = cur.next
else:
print(cur.coef, end=' ')
cur = cur.next
if cur.exp != 0:
print(cur.coef, cur.exp, end=' ')
else:
print(cur.coef,end=' ')
return ""
a = int(input())
X = list(map(int, input().split( )))
b = int(input())
Y = list(map(int, input().split( )))
def subPoly(X, Y):
x = getNode()
y = getNode()
result = getNode()
global a
global b
for i in range(0, (a*2), 2):
x.append(X[i], X[i+1])
for j in range(0, (b*2), 2):
y.append(Y[j], Y[j+1])
while (x.head != None and y.head != None):
if (x.head.exp > y.head.exp):
result.append(x.head.coef, x.head.exp)
x.head = x.head.next
elif (x.head.exp < y.head.exp):
result.append((-y.head.coef), y.head.exp)
y.head = y.head.next
else:
diff = x.head.coef - y.head.coef
if (diff != 0):
result.append(diff,x.head.exp)
x.head = x.head.next
y.head = y.head.next
else:
x.head = x.head.next
y.head = y.head.next
while (x.head != None):
result.append(x.head.coef, x.head.exp)
x.head = x.head.next
while (y.head != None):
result.append(-y.head.coef, y.head.exp)
y.head = y.head.next
return result.print_nd()
if (a*2==len(X) and b*2==len(Y)):
print(subPoly(X,Y))
有四个输入
class getNode:
def __init__(self,coef=None, exp=None, link=None):
self.head = None
self.coef = coef
self.exp = exp
self.next = None
self.size = 0
def append (self, coef, exp, link=None):
if self.size == 0:
self.head = getNode(coef, exp, link)
self.coef = coef
self.exp = exp
self.next = link
self.size += 1
else:
cur = self.head
while cur.next != None:
cur = cur.next
cur.next = getNode(coef, exp, link)
self.coef = coef
self.exp = exp
self.next = link
self.size += 1
def print_nd(self):
print(" ", end='')
cur = self.head
while cur.next != None:
if cur.exp != 0:
print(cur.coef, cur.exp, end=' ')
cur = cur.next
else:
print(cur.coef, end=' ')
cur = cur.next
if cur.exp != 0:
print(cur.coef, cur.exp, end=' ')
else:
print(cur.coef,end=' ')
return ""
a = int(input())
X = list(map(int, input().split( )))
b = int(input())
Y = list(map(int, input().split( )))
def subPoly(X, Y):
x = getNode()
y = getNode()
result = getNode()
global a
global b
for i in range(0, (a*2), 2):
x.append(X[i], X[i+1])
for j in range(0, (b*2), 2):
y.append(Y[j], Y[j+1])
while (x.head != None and y.head != None):
if (x.head.exp > y.head.exp):
result.append(x.head.coef, x.head.exp)
x.head = x.head.next
elif (x.head.exp < y.head.exp):
result.append((-y.head.coef), y.head.exp)
y.head = y.head.next
else:
diff = x.head.coef - y.head.coef
if (diff != 0):
result.append(diff,x.head.exp)
x.head = x.head.next
y.head = y.head.next
else:
x.head = x.head.next
y.head = y.head.next
while (x.head != None):
result.append(x.head.coef, x.head.exp)
x.head = x.head.next
while (y.head != None):
result.append(-y.head.coef, y.head.exp)
y.head = y.head.next
return result.print_nd()
if (a*2==len(X) and b*2==len(Y)):
print(subPoly(X,Y))
类getNode:
定义初始化(self,coef=None,exp=None,link=None):
self.head=无
self.coef=coef
self.exp=exp
self.next=无
self.size=0
def append(self、coef、exp、link=None):
如果self.size==0:
self.head=getNode(coef、exp、link)
self.coef=coef
self.exp=exp
self.next=链接
self.size+=1
其他:
cur=self.head
而现在,下一个!=无:
cur=cur.next
cur.next=getNode(coef、exp、link)
self.coef=coef
self.exp=exp
self.next=链接
self.size+=1
def打印(自身):
打印(“”,结束=“”)
cur=self.head
而现在,下一个!=无:
如果cur.exp!=0:
打印(cur.coef,cur.exp,end='')
cur=cur.next
其他:
打印(cur.coef,end='')
cur=cur.next
如果cur.exp!=0:
打印(cur.coef,cur.exp,end='')
其他:
打印(当前coef,结束=“”)
返回“”
a=int(输入())
X=list(映射(int,input().split())
b=int(输入())
Y=列表(映射(int,input().split())
def亚多边形(X,Y):
x=getNode()
y=getNode()
结果=getNode()
全球a
全球b
对于范围(0,(a*2),2)内的i:
x、 追加(x[i],x[i+1])
对于范围(0,(b*2),2)内的j:
y、 追加(y[j],y[j+1])
而(x.head!=无和y.head!=无):
如果(x.head.exp>y.head.exp):
result.append(x.head.coef,x.head.exp)
x、 head=x.head.next
elif(x.head.exp
请告诉我是否有任何可能的错误答案!谢谢:)