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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值