多项式相乘--洛谷AC

题目背景

编写一个程序实现两个一元多项式相乘。

输入格式

首先输入第一个多项式中系数不为0的项的系数和指数,以一个空格分隔。且该多项式中各项的系数均为0或正整数,系数和最高幂次不会超过int类型的表示范围。
对于多项式 anxn +a n-1 x n-1 + … + a1x1 + a0x0 的输入方法如下:
an n a n-1 n-1 … a1 1 a0 0
即相邻两个整数分别表示表达式中一项的系数和指数。在输入中只出现系数不为0的项。最后一项的指数后没有空格,只有一个回车换行符。
按照上述方式再输入第二个多项式。

输出格式

将运算结果输出到屏幕。将系数不为0的项按指数从高到低的顺序输出,每次输出其系数和指数,均以一个空格分隔,最后一项的指数后也可以有一个空格。
在这里插入图片描述
【特别注意】
本题如果要使用字符串读入两行数据,当使用gets()函数时,需注意在洛谷的在线IDE中,gets()会读入换行符并作为一个字符存到字符串里。因此,在本地IDE(如devc)里输出正确的,不一定代表在线评测时仍输出正确(可能会WA)。(本地和在线IDE编译运行不同)

#include<stdlib.h>
#include<iostream>
#include<string> 
#include<sstream>
#include<cstdio>
using namespace std;

typedef struct Polynode
{
    
    
	int coef;
	int exp;
	struct Polynode *next;
}Polynode,*Polylist;

Polylist Polysort(Polylist head)
{
    
    
	Polynode *first,*move,*p,*q;
	q=head;
	p=head->next;
	if(p==NULL) return head;
	first=p->next;
	p->next=NULL;
	move=first;
	while(move!=NULL)
	{
    
    
		first=first->next;
		if(p->exp==move->exp)
		{
    
    
			p->coef+=move->coef;
			free(move);
			if(p->coef==0)
			{
    
    
				q->next=p->next;
				free(p);
			}
		}
		else if(p->exp<move->exp)
		{
    
    
			head->next=move;
			move->next=p;
		}
		else if(p->next==NULL)
		{
    
    
			p->next=move;
			move->next=NULL;
		}
		else
		{
    
    
			q=p;
			p=p->next;
			while(1)
			{
    
    
				if(p->exp==move->exp)
				{
    
    
					p->coef+=move->coef;
					free(move);
					if(p->coef==0)
					{
    
    
						q->next=p->next;
						free(p);
					}
					break;
				}
				if(p->exp<move->exp)
				{
    
    
					q->next=move;
					move->next=p;
					break;
				}
				if(p->next==NULL)
				{
    
    
					p->next=move;
					move->next=NULL;
					break;
				}
				q=p;
				p=p->next;
			}
		}
		p=head->next;
		q=head;
		move=first;
	}
	return head;
}
Polylist creat()
{
    
    
	Polynode *head,*p,*newnode;
	head=(Polynode *)malloc(sizeof(Polynode));
	p=head;
	string str;
	getline(cin,str);
	int c,e;
	stringstream ss(str);
	while(ss>>c>>e)
	{
    
    
		newnode=(Polynode *)malloc(sizeof(Polynode));
		newnode->coef=c;
		newnode->exp=e;
		p->next=newnode;
		p=newnode;
	}
	p->next=NULL;
	return head;
}
Polylist Polymul(Polylist LA,Polylist LB)
{
    
    
	Polynode *head,*p,*q,*t,*newnode;
	p=LA->next;
	q=LB->next;
	head=(Polynode *)malloc(sizeof(Polynode));
	t=head;
	while(p!=NULL)
	{
    
    
		while(q!=NULL)
		{
    
    
			newnode=(Polynode *)malloc(sizeof(Polynode));
			t->next=newnode;
			t=t->next;
			t->coef=p->coef*q->coef;
			t->exp=p->exp+q->exp;
			q=q->next;
		}
		p=p->next;
		q=LB->next;
	}
	t->next=NULL;
	head=Polysort(head);
	return head;
}

int print(Polylist head)
{
    
    
	Polylist p;
	p=head->next;
	if(p==NULL) return 0;
	printf("%d %d ",p->coef,p->exp);
	while(p->next!=NULL)
	{
    
    
		p=p->next;
		printf("%d %d ",p->coef,p->exp);
	}
	return 0;
}

int main()
{
    
    
	Polylist LA,LB,LC;
	LA=creat();
	LB=creat();
	LC=Polymul(LA,LB);
	print(LC);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_45800653/article/details/107954678