题目

输入格式:
以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过 1000 的整数)。数字间以空格分隔。
输出格式:
以与输入相同的格式输出导数多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。注意“零多项式”的指数和系数都是 0,但是表示为 0 0。
输入样例:
3 4 -5 2 6 1 -2 0
输出样例:
12 3 -10 1 6 0
解题思路:
1.用一个列表来存储导数和指数,然后按要求输出
2.如果输出列表长度为0,print(“0 0”)。
代码一:
nmstr=input()
nmlist=[int(i) for i in nmstr.split()]
blist=[]
for i in range(0,len(nmlist),2):
a,b=nmlist[i],nmlist[i+1]
c=a*b
if b==0:
d=0
else:
d=b-1
if c!=0:
blist.append(c)
blist.append(d)
n=len(blist)
if n!=0:
for i in range(n):
if i==n-1:
print(blist[i],end="")
else:
print(blist[i],end=" ")
else:
print("0 0")
代码二:
nmstr=input()
nmlist=[int(i) for i in nmstr.split()]
blist=[]
for i in range(0,len(nmlist),2):
a,b=nmlist[i],nmlist[i+1]
c=a*b
if b==0:
d=0
else:
d=b-1
if c!=0:
blist.append(c)
blist.append(d)
if len(blist) != 0:
print(" ".join(str(i) for i in blist))
else:
print("0 0")
知识点:用空格连接各个元素输出列表:
blist=[12, 3, -10, 1, 6, 0]
print(" ".join(str(i) for i in blist))
本文介绍如何使用Python实现计算多项式导数的算法,通过示例解析输入格式如34-5261-20,并输出导数多项式的系数和指数,如123-10160。重点讲解了遍历输入、计算过程和输出格式的处理。

598

被折叠的 条评论
为什么被折叠?



