PTA一元多项式的乘法和加法c++

一元多项式的乘法和加法
题目:
设计函数分别求两个一元多项式的乘积与和。

输入格式:
输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。

输出格式:
输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0。
输入样例:
4 3 4 -5 2 6 1 -2 0
3 5 20 -7 4 3 1
输出样例:
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
作者
DS课程组
单位
浙江大学
代码长度限制
16 KB
时间限制
200 ms
内存限制
AC代码:

#include <bits/stdc++.h>
using namespace std;
typedef struct ploynode *ploy;
struct ploynode
{
	int coef;
	int expon;
	ploy link;
};
void attach(int c,int e,ploy *l){
	 ploy p;
	 p=(ploy)malloc(sizeof(struct ploynode));
	 p->coef=c;
	 p->expon=e;
	 p->link=NULL;
     (*l)->link=p;
     *l=p;
}
ploy readploy(){
	ploy p,rear,t;
	int c,e,N;
	cin>>N;
	if(!N) return NULL;
	p=(ploy)malloc(sizeof(struct ploynode));
	p->link=NULL;
	rear=p;
	while(N--){
		cin>>c>>e;
		attach(c,e,&rear);
	}
	t=p;p=p->link;free(t);
	return p;
}
ploy add(ploy p1,ploy p2){
	 if(!p1) return p2;
	 if(!p2) return p1;
	 ploy t1,t2,p,rear,t;
	 t1=p1; t2=p2;
	 p=(ploy)malloc(sizeof(struct ploynode));
	 p->link=NULL;
	 rear =p;
	   while(t1&&t2)
	 {
	 	 if(t1->expon==t2->expon){
	 	 	 int sum=t1->coef+t2->coef;
	 	 	 if(sum) attach(sum,t1->expon,&rear);
	 	 	 t1=t1->link;
	 	 	 t2=t2->link;
		  }
		  else if(t1->expon>t2->expon){
		  	   attach(t1->coef,t1->expon,&rear);
		  	   t1=t1->link;
		  }
		  else{
		  	  attach(t2->coef,t2->expon,&rear);
		  	   t2=t2->link;
		  }
	 }
	 while(t1){
	 	 attach(t1->coef,t1->expon,&rear);
	 	   t1=t1->link;
	 }
	 while(t2){
	 	 attach(t2->coef,t2->expon,&rear);
	 	    t2=t2->link;
	 }
	 t=p; p=p->link;free(t);
	 return p;
}
ploy mult(ploy p1, ploy p2) {
    if (!p1 || !p2) return NULL;
    ploy t1, t2, p, rear, t;
    t1 = p1;
    p = (ploy)malloc(sizeof(struct ploynode));
    p->link = NULL;
    rear = p;
    while (t1) {
        t2 = p2;
        while (t2) {
            attach(t1->coef*t2->coef, t1->expon + t2->expon, &rear);
            t2 = t2->link;
        }
        t1 = t1->link;
    }
    t = p; p = p->link; free(t);
    t1 = p;
    while (t1->link) {
        t2 = p;
        while (t2->link) {
            if (t2->expon < t2->link->expon) {
                swap(t2->coef,t2->link->coef);
                swap(t2->expon,t2->link->expon);
            }
            else if (t2->expon == t2->link->expon) {
                t = t2->link;
                t2->coef += t->coef;
                t2->link = t->link;
                free(t);
            }
            t2 = t2->link;
        }
        t1 = t1->link;
    }
    return p;
}
void printploy(ploy p){
	 if(!p){
	 	cout<<"0 0";
	 	return;
	 }
	 bool ans =true;
	 if(p->coef){
	 	 cout << p->coef<<' '<<p->expon;
	 	 ans = false;
	 }
	 p=p->link;
	 while(p){
	 	 if(p->coef){
	 	 	 cout<<' '<<p->coef<<' '<<p->expon;
	 	 	 ans =false;
		  }
		  p=p->link;
	 }
	 if(ans) cout<<"0 0";
}
int main(){
	ploy p,q,qq,p1;
	p=readploy();
    q=readploy();
    p1=mult(p,q);
    printploy(p1);
    cout<<endl;
    qq=add(p,q);
    printploy(qq);
	return 0;
}
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页