PTA 一元多项式的乘法与加法运算

PTA 一元多项式的乘法与加法运算

题目描述:

设计函数分别求两个一元多项式的乘积

输入格式:

输入分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
思路:

本题目为数据结构基础题,可以使用单链表进行存储多项式,在进行两个多项式的乘法以及加法时,通过两个指针来遍历两个多项式,根据多项式乘法与加法规则进行计算即可。
此外该题目多项式也可以通过二维数组进行存储,处理方法与单链表处理方法类似。
下面给出通过单链表实现该题目

代码1(未进行函数拆分):
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
typedef struct numnum{
	int xs,zs;
	struct numnum *next;
}num; 
int cf[10000];
int main(){
	num *head1,*head2,*ptr1,*ptr2;记录两个多项式 
	int printcount=0;
	head1=(num*)malloc(sizeof(struct numnum));
	head2=(num*)malloc(sizeof(struct numnum));
	head1->next=NULL;
	head2->next=NULL;
	int n1,n2;//记录两个非零多项式项数 
	//读入
	scanf("%d",&n1);
	ptr1=head1;
	for(int i=0;i<n1;i++){
		int a,b;
		scanf("%d%d",&a,&b);
		num *ptr=(num*)malloc(sizeof(struct numnum));
		ptr->xs=a;
		ptr->zs=b;
		ptr->next=NULL;
		ptr1->next=ptr;
		ptr1=ptr;
	} 
	scanf("%d",&n2);
	ptr2=head2;
	for(int i=0;i<n2;i++){
		int a,b;
		scanf("%d%d",&a,&b);
		num *ptr=(num*)malloc(sizeof(struct numnum));
		ptr->xs=a;
		ptr->zs=b;
		ptr->next=NULL;
		ptr2->next=ptr;
		ptr2=ptr;
	}
	if(n1==0&&n2==0)
	{
		printf("0 0\n0 0");
		return 0;
	}
//乘法
	printcount=0;
	int flag=0;
	int mzs=0;
	ptr1=head1;
	while(ptr1->next){
		ptr2=head2;
		while(ptr2->next){
			int a=ptr1->next->zs+ptr2->next->zs;
			int b=ptr1->next->xs*ptr2->next->xs;
			if(a>mzs)mzs=a;
			if(b==0) flag=1;
			cf[a]+=b;
			ptr2=ptr2->next;
		}
		ptr1=ptr1->next;
	}
	for(int i=mzs;i>=0;i--){	
		if(cf[i]!=0){
			if(printcount) printf(" ");
			printf("%d %d",cf[i],i),printcount++;
		}
	} 
		if(!printcount)
		printf("0 0");
	printf("\n");
//加法 
	flag=0;
	printcount=0;
	ptr1=head1;
	ptr2=head2;
	while(ptr1->next&&ptr2->next){
		if(ptr1->next->zs==ptr2->next->zs){
			if(ptr1->next->xs+ptr2->next->xs==0)flag=1;
			else{
				if(printcount) printf(" ");
				printf("%d %d",ptr1->next->xs+ptr2->next->xs,ptr1->next->zs),printcount++;
			}
			ptr1=ptr1->next;
			ptr2=ptr2->next;
		}
		else if(ptr1->next->zs>ptr2->next->zs){
			if(printcount) printf(" ");
			printf("%d %d",ptr1->next->xs,ptr1->next->zs);
			printcount++;
			ptr1=ptr1->next;
		}
		else{
			if(printcount) printf(" ");
			printf("%d %d",ptr2->next->xs,ptr2->next->zs);
			printcount++;
			ptr2=ptr2->next;
		}
	}
	if(ptr1->next==NULL)
		while(ptr2->next){
			if(printcount) printf(" ");
			printf("%d %d",ptr2->next->xs,ptr2->next->zs);
			printcount++;
			ptr2=ptr2->next;
		}
	else
		while(ptr1->next){
			if(printcount) printf(" ");
			printf("%d %d",ptr1->next->xs,ptr1->next->zs);
			printcount++;
			ptr1=ptr1->next;
		}
	if(!printcount)
		printf("0 0");
	return 0;
} 
代码2(进行了函数拆分):
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
typedef struct numnum{
	int xs,zs;
	struct numnum *next;
}num; 
int cf[10000];
num *head1,*head2,*ptr1,*ptr2;//记录两个多项式 
int n1,n2;//记录两个非零多项式项数 
void init(){  //初始化及读入
	head1 = (num*)malloc(sizeof(struct numnum));
	head2 = (num*)malloc(sizeof(struct numnum));
	head1->next = NULL;
	head2->next = NULL;
	scanf("%d",&n1);
	ptr1 = head1;
	for(int i = 0; i < n1; i++){
		int a,b;
		scanf("%d%d",&a,&b);
		num *ptr = (num*)malloc(sizeof(struct numnum));
		ptr->xs = a;
		ptr->zs = b;
		ptr->next = NULL;
		ptr1->next = ptr;
		ptr1 = ptr;
	} 
	scanf("%d",&n2);
	ptr2 = head2;
	for(int i=0; i<n2; i++){
		int a,b;
		scanf("%d%d",&a,&b);
		num *ptr = (num*)malloc(sizeof(struct numnum));
		ptr->xs = a;
		ptr->zs = b;
		ptr->next = NULL;
		ptr2->next = ptr;
		ptr2 = ptr;
	}	
}
void multiply_num(){//乘法
	int printcount = 0;
	int flag = 0;
	int mzs = 0;
	ptr1 = head1;
	while( ptr1->next ){
		ptr2 = head2;
		while(ptr2->next){
			int a = ptr1->next->zs + ptr2->next->zs;
			int b = ptr1->next->xs * ptr2->next->xs;
			if(a > mzs) 
				mzs = a;
			if(b == 0) 
				flag = 1;
			cf[a] += b;
			ptr2 = ptr2->next;
		}
		ptr1 = ptr1->next;
	}
	for(int i=mzs; i>=0; i--){	
		if(cf[i] != 0){
			if( printcount ) printf(" ");
			printf("%d %d",cf[i],i),printcount++;
		}
	} 
		if(!printcount)
		printf("0 0");
	printf("\n");
}
void add_num(){//加法 
	int flag = 0;
	int printcount = 0;
	ptr1 = head1;
	ptr2 = head2;
	while(ptr1->next && ptr2->next){
		if(ptr1->next->zs == ptr2->next->zs){
			if(ptr1->next->xs + ptr2->next->xs == 0 ) flag = 1;
			else{
				if( printcount ) printf(" ");
				printf("%d %d",ptr1->next->xs+ptr2->next->xs,ptr1->next->zs),printcount++;
			}
			ptr1 = ptr1->next;
			ptr2 = ptr2->next;
		}
		else if(ptr1->next->zs>ptr2->next->zs){
			if(printcount) printf(" ");
			printf("%d %d",ptr1->next->xs,ptr1->next->zs);
			printcount++;
			ptr1 = ptr1->next;
		}
		else{
			if(printcount) printf(" ");
			printf("%d %d",ptr2->next->xs,ptr2->next->zs);
			printcount++;
			ptr2 = ptr2->next;
		}
	}
	if(ptr1->next == NULL)
		while(ptr2->next){
			if(printcount) printf(" ");
			printf("%d %d",ptr2->next->xs,ptr2->next->zs);
			printcount++;
			ptr2 = ptr2->next;
		}
	else
		while(ptr1->next){
			if(printcount) printf(" ");
			printf("%d %d",ptr1->next->xs,ptr1->next->zs);
			printcount++;
			ptr1 = ptr1->next;
		}
	if(!printcount)
		printf("0 0");
}
int main(){
	init();
	if(n1 == 0 && n2 == 0){
		printf("0 0\n0 0");
		return 0;
	}
	multiply_num();
	add_num();
	return 0;
} 
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值