#include<stdio.h>
#include<stdlib.h>
typedef struct term {
int coef;
int exp;
struct term* next;
} term;
void printfpoly(term* polynomial) {
int first = 1;
term* p = polynomial;
while (p!=NULL) {
if (first == 1) {
printf("%d %d", p->coef, p->exp);
first = 0;
}else{
printf(" %d %d", p->coef, p->exp);
}
p = p->next;
}
}
term* derivative(term* polynomial) {
term* p = polynomial;
term* q = NULL;
if(p->next!=NULL){
while (p) {
if (p->exp == 0||p->coef==0) {
if (q) {
q->next = p->next;
}
else {
polynomial =p ->next;
}
p = p->next;
}
else {
p->coef *= p->exp;
p->exp -= 1;
q = p;
p = p->next;
}
}
}else{
if(p->exp==0){
p->coef=0;
}else{
p->coef=p->exp*p->coef;
p->exp--;
}
}
return polynomial;
}
term* makelist() {
term* head, * tail;
head = tail = NULL;
int coef, exp;
while (scanf("%d %d", &coef, &exp) == 2) {
term* p = (term*)malloc(sizeof(term));
p->coef = coef;
p->exp = exp;
p->next=NULL;
if (head == NULL) {
head = tail = p;
}
else {
tail->next = p;
tail = p;
}
}
return head;
}
int main() {
term* polynomial = makelist();
polynomial = derivative(polynomial);
printfpoly(polynomial);
return 0;
}
derivative()函数在实现时逻辑为:一:当多项式只有一项时,求导后为0多项式(1:指数为0,输出:0 0, 2:当系数为0,指数不为0,输出:0 求导后的指数。)
二:当多项式为至少两项时,在求导后为0多项式的要删去。本题系数为0时输出或不输出都为对