In ordinary cases, the unary n polynomial can be written as:
among them, pIis an index of eIthe non-0 coefficient of the item, and satisfies
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvdgvuz3dlaxr3/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">
so. We can use the linear table (definition: A linear table is a finite sequence of n data elements, such as arrays, vectors, linked lists, and so on) to represent:
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvdgvuz3dlaxr3/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">
Of the exponent I of each item can be represented by the ordinal of its coefficient pi.
in the usual application, the polynomial is relatively large, making the length of the linear table very difficult to determine, so we can consider the list, the vector can also (c + +). Example: If we use an array to represent the following polynomial:
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvdgvuz3dlaxr3/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">
Visible. We need an array of size 1549 to represent it. But the actual practical information has only 4 elements in the array, other places are 0, resulting in space waste. And assuming that we don't know the exponent of the highest order of the polynomial in advance, then we need to define a large enough array to store it, which obviously wastes a lot of memory space. We are able to use linked lists to solve the above problems:
Within the computer, we use a node to store a polynomial, in order to save space. and consistent with the writing habit. Only items that are not 0 coefficients are retained . Each node is divided into coefficients, exponents, and pointers to three fields, for example, as seen. The pointer next indicates the position of the next item.
Like what. The following polynomial is a-B, respectively:
Use the loop list to perform samples such as the following:
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvdgvuz3dlaxr3/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center ">
Two polynomial the addition of the arithmetic rules is very easy, for all exponents of the same item, the corresponding coefficients are added, if and not zero, then the composition and one of the polynomial. Copies all indices not identical to and polynomial. when implemented in detail, we use the above polynomial, a and a test example. A method of polynomial that can be stored by using another linked list. Or a method of merging a polynomial into a polynomial . For example, we will store the a+b and polynomial in a in a later way. Detailed program implementations such as the following (I used the Loop list):
#include <stdio.h> #include <stdlib.h> #include <conio.h>typedef struct pnode//use a linked list to store the polynomial information {float coef;//polynomial coefficients int exp;//polynomial exponential struct pnode *next;} Polynode;polynode *create () {float coef;int exp;polynode *head,*s,*r;head= (polynode*) malloc (sizeof (Polynode)); head- >coef=0;head->exp=-1;r=head;printf ("Please enter the coefficients and exponents of each item: \ n"), while (1) {scanf ("%f%d", &coef,&exp); if (coef!= 0)//Input 0-end input {s= (polynode*) malloc (sizeof (Polynode)); s->coef=coef;//s is used to save the current node s->exp=exp;r->next=s;r=s;} Elsebreak;} r->next=head;//Construction Cycle list return head;} Polynode*polyadd (polynode* pa,polynode* pb)//polynomial addition {Polynode *p,*q,*r,*s;float x;p=pa->next;//points to the first item of the polynomial respectively q=pb- >NEXT;S=PA;//S is used to save the current node while ((P!=PA) && (Q!=PB))//Does not end, back to the index of the chain header {if (p->exp<q->exp)//p is less than the exponent of Q, Put p into the linked list {s=p;p=p->next;} else if (P->EXP>Q->EXP) the exponent of//p is greater than the exponent of Q. Put Q into the linked list {r=q->next;q->next=p;s->next=q;s=q;q=r;} else//when both indices are the same. To merge {x=p->coef+q->coef;if (x!=0) {p->coef=x;s=p;} else//If the merge result is 0, remove the node {S->next=p-> Next;free (P);} P=s->next;r=q;q=q->next;free (R);}} if (Q!=PB)//Assuming that polynomial B has fewer items than polynomial a, {r=q;while (R->NEXT!=PB) R=r->next;s->next=q;r->next=pa;} Return PA;} void output (Polynode *head)//output polynomial information {Polynode *p;printf ("coefficients and exponents respectively:");p =head->next;while (p!=head) {printf ("%.1f, %d ", p->coef,p->exp);p =p->next;} printf ("\ n");} void Main () {polynode* ha,*hb;printf ("\ n establishes polynomial a:"), Ha=create (), Output (ha);p rintf ("\ nthe polynomial B:"); Hb=create (); Output ( HB); Ha=polyadd (HA,HB);p rintf ("\ n polynomial a+b:"); Output (ha);}
The results of the execution are as follows:
Original:http://blog.csdn.net/tengweitw/article/details/40476935
Nineheadedbird
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
"Introduction to Algorithms" Polynomial summation