设计函数求一元多项式的导数。
输入格式:
以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。
输出格式:
以与输入相同的格式输出导数多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。
输入样例:
3 4 -5 2 6 1 -2 0
输出样例:
12 3 -10 1 6 0
#include <stdio.h>
#include <stdlib.h>
struct data{
int x,y;
struct data *next;
};
struct data *create();
void compute(struct data *head);
void print(struct data *head);
main(){
struct data *head;
head=create();
compute(head);
print(head);
}
struct data *create(){
struct data *head,*title,*p;
head=(struct data*)malloc(sizeof(struct data));
head->next=NULL;
title=head;//带头结点
int a,b;
while(scanf("%d %d",&a,&b)==2&&a*b!=0){
if(a==0||b==0)
break;
p=(struct data*)malloc(sizeof(struct data));
p->x=a;
p->y=b;
title->next=p;
title=p;
}
title->next=NULL;
return head;
}
void compute(struct data *head){
struct data *p=head->next;
while(p!=NULL){
p->x=p->x*p->y;
p->y-=1;
p=p->next;
}
}
void print(struct data *head){
struct data *p=head->next;
if(p==NULL)
printf("0 0");
while(p!=NULL){
printf("%d %d",p->x,p->y);
if(p->next!=NULL)//后面还有数据要输出,不用另加空格
printf(" ");
p=p->next;
}
}