輸入格式: 輸入分2行,每行分別先給出多項式非零項的個數,再以指數遞降方式輸入一個多項式非零項繫數和指數(絕對值均為不超過1000的整數)。數字間以空格分隔。 輸出格式: 輸出分2行,分別以指數遞降方式輸出乘積多項式以及和多項式非零項的繫數和指數。數字間以空格分隔,但結尾不能有多餘空格。零多項式應輸 ...
輸入格式:
輸入分2行,每行分別先給出多項式非零項的個數,再以指數遞降方式輸入一個多項式非零項繫數和指數(絕對值均為不超過1000的整數)。數字間以空格分隔。
輸出格式:
輸出分2行,分別以指數遞降方式輸出乘積多項式以及和多項式非零項的繫數和指數。數字間以空格分隔,但結尾不能有多餘空格。零多項式應輸出0 0
。
輸入樣例:
4 3 4 -5 2 6 1 -2 0
3 5 20 -7 4 3 1
輸出樣例:
15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 1
5 20 -4 4 -5 2 9 1 -2 0
#include <stdio.h> #include <stdlib.h> //#include <dos.h> typedef struct polyNode{ int coef; int exp; struct polyNode *next; }polyNode, *polyList; void DestroyList(polyList L); void printList(polyList L); polyList creatList(int n); polyList add(polyList a, polyList b); polyList mul(polyList a, polyList b); int main() { int n1, n2, i; polyList a, b, L1, L2; scanf("%d", &n1); a = creatList(n1); scanf("%d", &n2); b = creatList(n2); L1 = mul(a, b); L2 = add(a, b); printList(L1); printf("\n"); printList(L2); DestroyList(L1); DestroyList(L2); //system("pause"); return 0; } void DestroyList(polyList L) { polyNode * tmp; while (L) { tmp = L->next; free(L); L = tmp; } } polyList creatList(int n) { polyNode *head, *r, *p; int coef, exp; head = (polyNode *)malloc(sizeof(polyNode)); r = head; while (n--) { scanf("%d%d", &coef, &exp); p = (polyNode *)malloc(sizeof(polyNode)); p->coef = coef; p->exp = exp; r->next = p; r = p; } r->next = NULL; return head; } polyList add(polyList a, polyList b) { polyNode *ha, *hb, *p, *r, *h; int temp; ha = a->next; hb = b->next; h = (polyNode *)malloc(sizeof(polyNode)); r = h; while (ha != NULL && hb != NULL) { p = (polyNode *)malloc(sizeof(polyNode)); if (ha->exp < hb->exp) { p->exp = hb->exp; p->coef = hb->coef; hb = hb->next; r->next = p; r = p; } else if (ha->exp > hb->exp) { p->exp = ha->exp; p->coef = ha->coef; ha = ha->next; r->next = p; r = p; } else { temp = ha->coef + hb->coef; if (temp != 0) { p->exp = ha->exp; p->coef = temp; r->next = p; r = p; } ha = ha->next; hb = hb->next; } } while ( hb != NULL ) { p = (polyNode *)malloc(sizeof(polyNode)); p->exp = hb->exp; p->coef = hb->coef; hb = hb->next; r->next = p; r = p; } while ( ha != NULL ) { p = (polyNode *)malloc(sizeof(polyNode)); p->exp = ha->exp; p->coef = ha->coef; ha = ha->next; r->next = p; r = p; } r->next = NULL; DestroyList(a); DestroyList(b); return h; } polyList mul(polyList a, polyList b) { polyNode *ha, *hb , *r, *p; polyList c, tempc; ha = a->next; hb = b->next; c = (polyNode *)malloc(sizeof(polyNode)); c->next = NULL; if (ha == NULL || hb == NULL) { return c; } while (ha != NULL) { tempc = (polyNode *)malloc(sizeof(polyNode)); r = tempc; hb = b->next; while (hb != NULL) { p = (polyNode *)malloc(sizeof(polyNode)); p->exp = ha->exp + hb->exp; p->coef = ha->coef * hb->coef; hb = hb->next; r->next = p; r = p; } r->next = NULL; c = add(c,tempc); ha = ha->next; } return c; } void printList(polyList L) { polyNode * x; x = L->next; if (x == NULL) { printf("0 0"); } while (x != NULL) { if (x->next == NULL) { printf("%d %d", x->coef, x->exp); } else printf("%d %d ", x->coef, x->exp); x = x->next; } }