Pat乙级1010题——一元多项式求导(Python)

设计函数求一元多项式的导数。(注:x^n^(n为整数)的一阶导数为n*x^n-1^。)

输入格式:以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。

输出格式:以与输入相同的格式输出导数多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。注意“零多项式”的指数和系数都是0,但是表示为“0 0”。

输入样例:

3 4 -5 2 6 1 -2 0

输出样例:

12 3 -10 1 6 0

答案如下

def deravition():
    numList = []
    numList = map(int, raw_input().split())
    resultList = []
    for i in range(len(numList)):
        if (i%2) == 0 and numList[i+1] != 0:
            resultList.append(numList[i] * numList[i+1])
            resultList.append(numList[i+1] - 1)
        #notice the consequence that only one given number
        elif numList[1] == 0:
            print '0 0'
            return
    #the format of output must meet the requirement     
    print  ' '.join(map(str,resultList))    
if __name__ == '__main__':
    deravition()

猜你喜欢

转载自blog.csdn.net/coder_wu/article/details/81843122