23-4-25--链表--一元多项式求导

发布时间 2023-04-25 21:10:33作者: Daniel350

设计函数求一元多项式的导数。

输入格式:

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

输出格式:

以与输入相同的格式输出导数多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。

输入样例:

3 4 -5 2 6 1 -2 0
 

输出样例:

12 3 -10 1 6 0

代码如下:

#include <iostream>

using namespace std;

typedef struct node{
    int c;
    int e;
    struct node *next;
}linklist;
typedef struct node* plink;

plink addnode(plink end,int c,int e)
{
    plink t=new linklist;
    t->c =c;
    t->e =e;
    t->next =NULL;
    end->next =t;
    return t;
}
void printnode(plink head)
{
    plink p=head->next ;
    if(p)
    {
        printf("%d %d",p->c ,p->e );
        p=p->next ;
        while(p)
        {
            printf(" %d %d",p->c ,p->e );
            p=p->next ;
        }
    }else{
        printf("0 0");
    }
}

int main()
{
    int c,e;
    plink head=new linklist;
    plink end=head;
    head->next =NULL;
    while(cin>>c>>e)
    {
        int tc,te;
        if(e!=0)
        {
            te=e-1;
            tc=e*c;
            if(tc!=0)
            {
                end=addnode(end,tc,te);
            }
        }
    }
    printnode(head);
    return 0;
    
    
}

结果如下: