The product and sum of the two unary polynomials are obtained by the design function respectively.
Input format:
Input is divided into 2 lines, each line is given the number of non-0 polynomial, and then the exponential degradation way to enter a polynomial non-0 coefficients and exponents (absolute value is not more than 1000 integers). The numbers are separated by a space.
Output format:
The output is divided into 2 rows, and the coefficients and exponents of the product polynomial and the polynomial non-0 are output in exponential degradation mode respectively. The numbers are separated by a space, but cannot have extra spaces at the end. The 0 polynomial should be output 0 0
.
Input Sample:
4 3 4 -5 2 6 1 -2 03 5 20 -7 4 3 1
Sample output:
15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 15 20 -4 4 -5 2 9 1 -2 0
1#include <cstdio>2#include <cstdlib>3 4typedefstructPolynode *Ptrtopolynode;5 typedef ptrtopolynode Position;6 structPolynode {7 intc, E;8 position next;9 };Ten typedef ptrtopolynode list; One A //polynomial read-in functions - List Readpoly () { - intN, c, E; thescanf"%d", &n); -List p = (list)malloc(sizeof(structPolynode)); -P->next =NULL; -Position rear =p; + while(n--){ -List temp = (list)malloc(sizeof(structPolynode)); +scanf"%d%d", &temp->c, &temp->e); ATemp->next =NULL; atRear->next =temp; -Rear =temp; - } -List t=p; p=p->next; Free(t); - returnp; - } in - //Polynomial output function to voidprint (list p) { + intflag=0; - if(p==null) printf ("0 0");//0 Polynomial special treatment the Else{ * while(p!=NULL) { $ if(flag==0) flag=1;Panax Notoginseng Elseprintf" "); - if(p->c==0&&p->e==0) flag=2; the Elseprintf"%d%d", P->c, p->e); +p = p->Next; A } the } +printf"\ n"); - } $ $ //determine where temp is inserted based on the size of the index - voidAttach (list p, list temp1) { -Position pre=p, rear=p->Next; the while(rear->e>temp1->e) {//rear in the first position that is not above the temp index, or the end of the list jumps out of a loop -Pre =Rear;Wuyi if(rear->next==NULL) { theRear=NULL; - Break; Wu } -Rear = rear->Next; About } $ if(rear==NULL) { -Temp1->next =NULL; -Pre->next =Temp1; - } A Else if(rear->e==temp1->e) {//merger of similar items +Rear->c + = temp1->C; the if(rear->c==0){ -Pre->next = rear->Next; $ } the } the Else{ theTemp1->next =Rear; thePre->next =Temp1; - } in } the the //polynomial multiplication function About list Multiply (list P1, list p2) { theList mult = (list)malloc(sizeof(structPolynode)); theMult->next =NULL; the if(P1==null | | p2==null) {//P1,P2 has one or more empty, the product must be 0 polynomial +mult=NULL; - } the Else{ Bayi position rear1, rear2; theREAR1=P1; Rear2=P2; the while(rear1) { - while(rear2) { -List temp = (list)malloc(sizeof(structPolynode)); theTemp->c = rear1->c*rear2->C; theTemp->e = rear1->e+rear2->e; the if(temp->c==0) temp->e=0; the if(Mult->next==null) {//after the first node is inserted into the head node -Temp->next =NULL; theMult->next =temp; the } the ElseAttach (mult, temp);94REAR2 = rear2->Next; the } theREAR2 =P2; theREAR1 = rear1->Next;98 } AboutList t = mult; mult = mult->next; Free(t); - }101 returnmult;102 }103 104 //Addition implementation function the list polysum (list P1, list p2) {106List sum = (list)malloc(sizeof(structpolynode));107 //two polynomial one of which is 0 polynomial, then equals another polynomial108 if(p1==null) sum->next=P2; 109 Else if(p2==null) sum->next=P1; the Else{111 //two polynomial is not 0 polynomial, only need to insert P2 one by one into the P1, attach function has been processed in the same class merge theSum->next =P1;113 for(list rear=p2; rear; rear=rear->next) { theList temp = (list)malloc(sizeof(structpolynode)); theTemp->c = rear->c; Temp->e = rear->e; Temp->next =NULL; the Attach (sum, temp);117 }118 }119List t=sum; sum = sum->next; Free(t); - returnsum;121 }122 123 //Program Framework124 intMain () { the list P1, p2, mult, sum;126P1 =Readpoly ();127P2 =Readpoly (); -Mult =Multiply (P1, p2);129sum =Polysum (P1, p2); the print (mult);131 print (sum); the return 0;133}
Multiplication and addition operation of 02-linear structure 21-tuple polynomial