PAT(乙级)一元多项式求导python

本文介绍如何使用Python实现计算多项式导数的算法,通过示例解析输入格式如34-5261-20,并输出导数多项式的系数和指数,如123-10160。重点讲解了遍历输入、计算过程和输出格式的处理。
摘要由CSDN通过智能技术生成

题目

在这里插入图片描述

输入格式:

以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过 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))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码力全开666

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值