编写求两个多项式相加的程序。 实验数据: P1(x)=5x8+3x4-2x3+3x+5 P2(x)=7x4+2x3-2x2 P(x)=P1(x)+P2(x)=5x8 +10x4-2x2+3x+5
时间: 2023-09-28 17:03:21 浏览: 153
```python
def add_poly(p1, p2):
result = {}
for exp, coeff in p1.items():
result[exp] = coeff + result.get(exp, 0)
for exp, coeff in p2.items():
result[exp] = coeff + result.get(exp, 0)
return result
p1 = {8: 5, 4: 3, 3: -2, 5: 3}
p2 = {4: 7, 3: 2, 2: -2}
p3 = add_poly(p1, p2)
print(p3) # {8: 5, 4: 10, 3: 0, 5: 3, 2: -2}
```
阅读全文