02-线性结构2. 一元多项式求导 (25)
时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
设计函数求一元多项式的导数。(注:xn(n为整数)的一阶导数为n*xn-1。)
输入格式:以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。
输出格式:以与输入相同的格式输出导数多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。注意“零多项式”的指数和系数都是0,但是表示为“0 0”。
输入样例:
3 4 -5 2 6 1 -2 0
输出样例:
12 3 -10 1 6 0
#include<stdio.h> #include<stdlib.h> #include <malloc.h> typedef struct Node { int dex; int cof; struct Node * Next; }node; int main() { //Qlink newLink; //newLink=(Link*)malloc(sizeof(Link)); struct Node *head,*tail; head=(node*)malloc(sizeof(node)); tail=(node*)malloc(sizeof(node)); tail->Next=NULL; head->Next=tail; struct Node *pthis,*pthat; pthis=head;pthat=pthis; //输入参数 int dex,cof; while(scanf("%d %d",&dex,&cof)){ pthis=(node*)malloc(sizeof(node)); pthis->dex=dex;pthis->cof=cof; pthis->Next=pthat->Next; pthat->Next=pthis; pthat=pthis; if(getchar()==‘\n‘)break; } //遍历 pthat=head->Next; if(pthat->Next->Next==NULL&&pthat->cof==0) { printf("0 0\n"); } else{ while(pthat!=NULL&&pthat->Next!=NULL) { pthat->dex=pthat->dex*pthat->cof; pthat->cof-=1; if(pthat->dex!=0){ printf("%d %d",pthat->dex,pthat->cof); if(pthat->Next->Next!=NULL&&pthat->Next->dex*pthat->Next->cof!=0)printf(" "); else printf("\n"); } pthat=pthat->Next; } } //free(pthat); free(pthis);free(head);free(tail); return 0; }
时间: 2024-12-29 23:56:05