需要注意的是:在模拟求导的时候,对常数求导时,只要把前面的系数赋成0就行了,不要动指数,从而通过测试点样例0 0
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int coef;
int exp;
struct Node* next;
} Node;
typedef struct {
Node* head;
} List;
void add(List* pList, int coef, int exp) {
Node* p = (Node*)malloc(sizeof(Node));
p->coef = coef;
p->exp = exp;
p->next = NULL;
Node* last = pList->head;
if(last) {
while(last->next) last = last->next;
last->next = p;
}else {
pList->head = p;
}
}
void derivation(List* pList) {
for(Node* p = pList->head; p; p = p->next) {
if(p->exp == 0) {
p->coef = 0;
break;
}
p->coef = (p->coef)*(p->exp);
p->exp = p->exp - 1;
}
}
void print(List* pList) {
for(Node* p = pList->head; p; p = p->next) {
if(p->next == NULL || p->next->coef == 0) {
printf("%d %d", p->coef, p->exp);
break;
}
printf("%d %d ", p->coef, p->exp);
}
}
int main() {
List list;
list.head = NULL;
int exp,coef;
while(scanf("%d %d", &coef, &exp) == 2) {
add(&list, coef, exp);
}
derivation(&list);
print(&list);
return 0;
}