chenxi16

导航

数据结构-1 02-线性结构2 一元多项式的乘法与加法运算

 

 

  1 #include<stdio.h>
  2 #define MAXN 2010
  3 struct poly{
  4     int exp,var;
  5 };
  6 struct poly a[MAXN],b[MAXN],c[MAXN];
  7 void read(struct poly a[]);
  8 void multiply();
  9 void add();
 10 int main(){
 11     int n,i;
 12     read(a);
 13     read(b);
 14     
 15     multiply();
 16     for(i=0;i<MAXN;i++){
 17         c[i].exp = 0;
 18         c[i].var = 0;
 19     }
 20     add();
 21     return 0;
 22 }
 23 
 24 void read(struct poly a[]){
 25     int i,n;
 26     scanf("%d",&n);
 27     a[0].exp = n;
 28     for(i=1;i<=n;i++){
 29         scanf("%d %d",&a[i].var,&a[i].exp);
 30     }
 31 }
 32 void multiply(){
 33     int i,n,j,flag=0,k,count=0;
 34     int e;
 35     int v;
 36     
 37     count=1;
 38     for(i=1;i<=b[0].exp;i++){
 39         e = a[1].exp + b[i].exp;
 40         v = a[1].var * b[i].var;
 41         if(v!=0){
 42             c[count].exp = e;
 43             c[count].var = v;
 44             count++;
 45         }
 46     }
 47     c[0].exp = count-1;
 48     for(j=2;j<=a[0].exp;j++){
 49         for(i=1;i<=b[0].exp;i++){
 50             flag = 0;
 51             e = a[j].exp + b[i].exp;
 52             v = a[j].var * b[i].var;
 53             if(v!=0){
 54                 for(k=1;k<=c[0].exp;k++){
 55                     if(c[k].exp==e){
 56                         flag = 1;
 57                         c[k].var += v;
 58                     }
 59                 }
 60                 if(flag==0){
 61                     c[0].exp++;
 62                     c[c[0].exp].exp = e;
 63                     c[c[0].exp].var = v;
 64                 }
 65             }  
 66         }
 67     }
 68     count = 0;
 69     for(i=1;i<=c[0].exp;i++){
 70         count=i;
 71         for(j=i;j<=c[0].exp;j++){
 72             if(c[count].exp < c[j].exp) count=j;
 73         }
 74         if(count!=i){
 75             e = c[i].exp;
 76             c[i].exp = c[count].exp;
 77             c[count].exp = e;
 78 
 79             v = c[i].var;
 80             c[i].var = c[count].var;
 81             c[count].var = v;
 82         }
 83     }
 84     
 85     if(c[0].exp==0) printf("0 0");
 86     else{
 87         for(i=1;i<=c[0].exp;i++){
 88             if(c[i].var!=0) {
 89                 printf("%d %d",c[i].var,c[i].exp);
 90                 if(i!=c[0].exp) printf(" ");
 91             }
 92         }
 93     }
 94     printf("\n");
 95 }
 96 void add(){
 97     int i,j,count=0;
 98     int v;
 99     i=1;
100     j=1;
101     while(i<=a[0].exp && j<=b[0].exp){
102         if(a[i].exp > b[j].exp){
103             count++;
104             c[count].exp = a[i].exp;
105             c[count].var = a[i].var;
106             i++;
107         }
108         else if(a[i].exp == b[j].exp){
109             v = a[i].var + b[j].var;
110             if(v!=0){
111                 count++;
112                 c[count].exp = a[i].exp;
113                 c[count].var = v;
114             }
115             i++;j++;
116         }
117         else{
118             count++;
119             c[count].exp = b[j].exp;
120             c[count].var = b[j].var;
121             j++;
122         }
123     }
124     while(i<=a[0].exp){
125         count++;
126         c[count].exp = a[i].exp;
127         c[count].var = a[i].var;
128         i++;
129     }
130     while(j<=b[0].exp){
131         count++;
132         c[count].exp = b[j].exp;
133         c[count].var = b[j].var;
134         j++;
135     }
136     c[0].exp = count;
137     if(c[0].exp==0) printf("0 0");
138     else{
139         for(i=1;i<=c[0].exp;i++){
140             printf("%d %d",c[i].var,c[i].exp);
141             if(i!=c[0].exp) printf(" ");
142         }
143     }
144     printf("\n");
145 
146    
147 }

 

 

posted on 2020-03-31 17:35  chenxi16  阅读(197)  评论(0编辑  收藏  举报