#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
typedef int DataType;
typedef struct SqList{
DataType z[100];
DataType data[100];
int length;
}SqList;
void CreateList(SqList *&L,DataType a[],int n){
int i=0,k=0;
L=(SqList *)malloc(sizeof(SqList));
while(i<n){
L->z[k]=a[i];
i++;
L->data[k]=a[i];
k++;i++;
}
L->length=k-1;
}
void ListInsert(SqList *&L,int x,int y){
int len=L->length;
L->z[len]=x;
L->data[len]=y;
L->length++;
}
void DisList(SqList *L){
for(int i=0;i<L->length;i++)
cout<<L->z[i]<<" "<<L->data[i]<<" ";
cout<<endl;
}
void add(SqList *a,SqList *b,SqList *&c){
int i=0,j=0;
while(i<a->length&&j<b->length){
int x1=a->z[i];
int y1=a->data[i];
int x2=b->z[j];
int y2=b->data[j];
if(x1==x2){
ListInsert(c,x1,y1+y2);
i++;
j++;
}
else if(x1<x2){
ListInsert(c,x1,y1);
i++;
}
else{
ListInsert(c,x2,y2);
j++;
}
}
while(i<a->length) ListInsert(c,a->z[i],a->data[i]),i++;
while(j<b->length) ListInsert(c,b->z[j],b->data[j]),j++;
}
void mul(SqList *a,SqList *b,SqList *&c){
int len1=a->length;
int len2=b->length;
int p[100];
for(int i=0;i<90;i++) p[i]=0;
for(int i=0;i<len1;i++){
for(int j=0;j<len2;j++){
int x1=a->z[i];
int y1=a->data[i];
int x2=b->z[j];
int y2=b->data[j];
int x=x1+x2;
int y=y1*y2;
p[x]+=y;
}
}
for(int i=0;i<90;i++)
if(p[i]) ListInsert(c,i,p[i]);
}
int main(){
SqList *t1,*t2,*t3,*t4;
int a[100];
int n=0;
while(cin>>a[n]){
if(a[n]==0&&a[n-1]==0&&n) break;
n++;
}
CreateList(t1,a,n);
n=0;
while(cin>>a[n]){
if(a[n]==0&&a[n-1]==0&&n) break;
n++;
}
CreateList(t2,a,n);
t3=(SqList *)malloc(sizeof(SqList));
t4=(SqList *)malloc(sizeof(SqList));
/*
DisList(t1);
DisList(t2);
*/
add(t1,t2,t3);
cout<<endl;
DisList(t3);
cout<<endl;
mul(t1,t2,t4);
DisList(t4);
cout<<endl;
return 0;
}
第三次试验
最新推荐文章于 2022-04-18 10:54:35 发布