The design function calculates the product and sum of two univariate polynomials respectively.
Input format:
The input is divided into two rows, each of which gives the number of non-zero polynomial terms, and then an exponential descending polynomial coefficients and exponents (absolute values are not more than 1000 integers). Numbers are separated by spaces.
Output format:
The output is divided into two lines, exponentially decreasing the coefficients and exponents of the product polynomials and the nonzero terms of the polynomials. Numbers are separated by spaces, but no extra spaces are allowed at the end. The zero polynomial should output 0.
Input sample: 4 3 4 -5 2 6 1 -2 0 3 5 20 -7 4 3 1 Output sample: 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
struct node{ int coef; //coefficient int expn; //index node* next; };
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; struct node{ int coef; //coefficient int expn; //index node* next; }; node* creat_list(int n){ //Read linked list node *head, *r; head = new node; r = head; int coef , expn; while(n--){ scanf("%d%d",&coef,&expn); node* tmp = new node; //Create temporary nodes tmp->coef = coef; tmp->expn = expn; r->next = tmp; //Temporary nodes are connected to the list r = tmp; } r->next = NULL; //Set the end to NULL return head; } node* add_list(node* a,node* b){ node *r,*fans, *ans; node *ha,*hb; //To prevent modifying the value of the pointer itself, the proxy pointer is used to complete the operation, that is, the cursor. fans = new node; ans = fans; //ans as the "cursor" of fans“ ha = a->next; hb = b->next; while(ha && hb){ node* tmp = new node; //Set up once. if(ha->expn > hb->expn){ //Add a larger exponent to the list fans each time tmp->coef = ha->coef; tmp->expn = ha->expn; ans->next = tmp; ans = tmp; ha = ha->next; } else if(ha->expn < hb->expn){ tmp->coef = hb->coef; tmp->expn = hb->expn; ans->next = tmp; ans = tmp; hb = hb->next; } else{ int mulOfcoef = (ha->coef)+(hb->coef); //If the exponents are the same, sum the coefficients. if(mulOfcoef!=0){ tmp->coef = mulOfcoef; tmp->expn = ha->expn; ans->next = tmp; ans = tmp; } ha = ha->next; //Note that even if the sum is zero, you have to move the cursor. hb = hb->next; } } while(ha){ node* tmp = new node; tmp->coef = ha->coef; tmp->expn = ha->expn; ans->next = tmp; ans = tmp; ha = ha->next; } while(hb){ node* tmp = new node; tmp->coef = hb->coef; tmp->expn = hb->expn; ans->next = tmp; ans = tmp; hb = hb->next; } ans->next = NULL; //Set the end to NULL return fans; } node* multi_list(node* a,node* b){ node* ha, *hb; node* ans,*fans; ha = a->next; hb = b->next; fans = creat_list(0); if(ha == NULL || hb == NULL){ return fans; } node* tmp; while(ha != NULL){ tmp = new node; ans = tmp; hb = b->next; //Every time we multiply from the first item of b. while(hb != NULL){ node* ltmp = new node; ltmp->expn = ha->expn + hb->expn; //Exponential addition and coefficient multiplication ltmp->coef = ha->coef * hb->coef; hb = hb->next; ans->next= ltmp; ans = ltmp; } ans->next = NULL; fans = add_list(fans,tmp); //Decomposition multiplication into additions ha = ha->next; } return fans; } void print_list(node* l){ node *hc; int flag = 0; hc = l->next; //Pointer operations are commonly used, replacing source node operations with newly created nodes if(hc == NULL){ //Format control. Real pit! printf("0 0"); } while(hc != NULL){ if(flag) printf(" "); else flag = 1; printf("%d %d",hc->coef,hc->expn); hc = hc->next; } } int main(){ int n; scanf("%d",&n); node *a = creat_list(n); int m; scanf("%d",&m); node* b = creat_list(m); node* c = add_list(a,b); node* d = multi_list(a,b); print_list(d); printf("\n"); print_list(c); printf("\n"); return 0; }