设计函数求一元多项式的导数。
输入格式:
以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。
输出格式:
以与输入相同的格式输出导数多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。
输入样例
3 4 -5 2 6 1 -2 0
输出样例
12 3 -10 1 6 0
代码:
#include<iostream>
#include<queue>
#include<stack>
#include<cstring>
#include<stdlib.h>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<map>
#include<stdio.h>
#include<string>
#define LL long long
using namespace std;
#define MAXN 1000011
#define sd1(i) scanf("%d", &i)
#define sd2(i, j) scanf("%d%d", &i, &j)
#define sl2(i, j) scanf("%lld%lld", &i, &j)
#define sd3(i, j, k) scanf("%d%d%d", &i, &j, &k)
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define NULL 0
typedef int ElemType;
typedef int Status;
typedef struct node{
int co, index;
}SElemType;
typedef struct LNode{
SElemType data;
struct LNode *next;
}LNode,*LinkList;
Status ListCreat_L(LinkList &L){
LNode *realPtr, *curPtr;
L = (LNode*) malloc(sizeof(LNode));
if(!L) exit(OVERFLOW);
L -> next = NULL;
realPtr = L;
int a, b;
while(sd1(a) != EOF){
sd1(b);
curPtr = (LNode*) malloc(sizeof(LNode));
if(!curPtr) exit(OVERFLOW);
curPtr -> data.co = a;
curPtr -> data.index = b;
curPtr -> next = NULL;
realPtr -> next = curPtr;
realPtr = curPtr;
}
return OK;
}
void ListPrint(LinkList &L){
if(L->next == NULL){
cout << "0 0" << endl;
return;
}
LNode *p;
p = L -> next;
while(p){
if(p -> next != NULL) cout << p -> data.co <<' ' << p ->data.index << ' ';
else cout << p -> data.co << ' ' << p ->data.index << endl;
p = p -> next;
}
}
Status ListDerivation(LinkList &L){
LNode *p;
p = L;
while(p ->next){
if(p ->next-> data.index == 0) {
LNode *q;
q = p ->next;
p -> next = q -> next;
free(q);
continue;//终止跳出循环 不然后面报异常
}
p -> next-> data.co = p -> next-> data.index * p -> next-> data.co;
p ->next-> data.index = p ->next-> data.index - 1;
if(p ->next->data.co == 0){
LNode *q;
q = p ->next;
p -> next = q -> next;
free(q);
}
p = p -> next; //后移
//ListPrint(L);
}
return OK;
}
int main(){
LinkList L;
if(ListCreat_L(L)!=OK) cout <<"wrong" << endl;
//ListPrint(L);
if(ListDerivation(L)!=OK) cout << "wrong" << endl;
ListPrint(L);
system("pause");
return 0;
}
这篇博客探讨了如何设计一个函数来计算一元多项式的导数,输入和输出都以指数递降的链表形式表示。内容涉及到数学和算法,特别是针对多项式操作的链表数据结构的应用。

976

被折叠的 条评论
为什么被折叠?



