标签:
设计函数求一元多项式的导数。
输入格式:以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。
输出格式:以与输入相同的格式输出导数多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。注意“零多项式”的指数和系数都是0,但是表示为“0 0”。
输入样例:
3 4 -5 2 6 1 -2 0
输出样例:
12 3 -10 1 6 0
#include <iostream> #include <string> #include <stdlib.h> using namespace std; typedef struct node{ int coef; int expon; struct node *next; } *PtrPolyNode, PolyNode; PtrPolyNode createList(); void deriPoly( PtrPolyNode headNode ); void printPoly( PtrPolyNode headNode ); int main() { PtrPolyNode headNode; headNode = createList(); deriPoly( headNode ); printPoly( headNode ); return 0; } PtrPolyNode createList() { PtrPolyNode headNode = NULL, currentNode = NULL, prevNode = NULL; do { currentNode = (PtrPolyNode)malloc( sizeof( PolyNode ) ); cin >> currentNode->coef >> currentNode->expon; currentNode->next = NULL; if ( headNode == NULL ) { headNode = currentNode; prevNode = currentNode; continue; } prevNode->next = currentNode; prevNode = currentNode; } while ( cin.get() != ‘\n‘ ); return headNode; } void deriPoly( PtrPolyNode headNode ) { PtrPolyNode prev, cur; cur = prev = headNode; if ( headNode->next == NULL && headNode->expon == 0 ) //point { headNode->coef = 0; return; } while( cur != NULL ) { if ( cur->expon == 0 ) //point { prev->next = NULL; free( cur ); break; } cur->coef = cur->coef * cur->expon; cur->expon = cur->expon - 1; prev = cur; cur = cur->next; } return; } void printPoly( PtrPolyNode headNode ) { PtrPolyNode ptr; ptr = headNode; while ( ptr != NULL ) { if ( ptr->next == NULL ) { cout << ptr->coef << ‘ ‘ << ptr->expon; } else { cout << ptr->coef << ‘ ‘ << ptr->expon << ‘ ‘; } ptr = ptr->next; } return; }
标签:
原文地址:http://www.cnblogs.com/liangchao/p/4271176.html