my output:
12x^4 + 9x^3 - 5x^2 - 1x - 1
excepted output:
12x^4 + 9x^3 - 5x^2 - x - 1
For term Cix^Pi, if the coefficient of the term Ci is 1, simply print x^Pi instead of 1x^Pi.
N = int(input())
A =[0 for item in range(N)]
for item in range(N):
p, c = input().split(' ')
A[int(p)] = int(c)
M = int(input())
B =[0 for item in range(M)]
for item in range(M):
p, c = input().split(' ')
B[int(p)] = int(c)
pol_max , pol_min = A , B
if len(pol_min) > len(pol_max):
pol_max , pol_min = B , A
for item in range(len(pol_min)):
pol_max[item] += pol_min[item]
for item in range(len(pol_max)-1,0,-1):
if item == 0:
continue
if pol_max[item]==1 or pol_max[item]==-1 :
print (f'x^{item} + ',end='')
# elif pol_max[item]<0:
# print (f'{pol_max[item]}x^{item} ',end='')
else:
print (f'{pol_max[item]}x^{item} + ',end='')
print(pol_max[0])
Comments
Leave a comment