二元多项式
Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^
题目描述
给你多个二元多项式和一个操作符,让你输出操作符操作这些二元多项式之后的结果。
输入
首先输入二元多项式的个数n和操作符号(‘+’,‘*’);
后面n行输入每一个多项式。
多组输入,当n=0的时候结束输入。
(n<5,二元多项式的长度小于1000,二元多项式都是由x,y,^,数字,’+’组成的)
输出
输出操作之后的结果。
(输出的顺序按照:x^2>x>xy^2>xy>y^2>y>常数)
示例输入
2 + 3x+4y^2+3xy+6x^10y^2+1 2x+6y 0
示例输出
6x^10y^2+5x+3xy+4y^2+6y+1
提示
//这是群里的, 不是自己写的 ~~~~转载
#include <bits/stdc++.h>
struct node
{
int x,y,num;
struct node *next;
} head[9];
char st[1024];
struct node *creat()
{
struct node *p;
p=(struct node*)malloc(sizeof(struct node));
p->x=0,p->y=0,p->num=0;
p->next=NULL;
return p;
};
void build(struct node *root)
{
int len,i,data,temp;
struct node *p;
len=strlen(st);
data=0,temp=1;
p=creat();
for(i=len-1; i>=0; i--)
{
if(st[i]<='9'&&st[i]>='0')
{
data=(st[i]-'0')*temp+data;
temp*=10;
}
if(st[i]=='x')
{
if(data)
p->x=data;
else
p->x=1;
data=0,temp=1;
}
if(st[i]=='y')
{
if(data)
p->y=data;
else
p->y=1;
data=0,temp=1;
}
if(st[i]=='+')
{
if(data)
p->num=data;
else if(p->x||p->y)
p->num=1;
data=0,temp=1;
p->next=root->next;
root->next=p;
p=creat();
}
}
if(data)
p->num=data;
else if(p->x||p->y)
p->num=1;
p->next=root->next;
root->next=p;
}
void link(struct node*ROOT,struct node*root)
{
struct node *p,*q,*head,*t;
p=ROOT->next;
head=creat();
while(p)
{
q=root->next;
while(q)
{
t=creat();
t->num=p->num*q->num;
t->x=p->x+q->x;
t->y=p->y+q->y;
t->next=head->next;
head->next=t;
q=q->next;
}
p=p->next;
}
ROOT->next=head->next;
free(head);
}
void bing(struct node *root)
{
struct node *p,*q,*tail;
p=root->next;
while(p)
{
tail=p;
q=tail->next;
while(q)
{
if(p->x==q->x&&p->y==q->y)
{
p->num+=q->num;
tail->next=q->next;
free(q);
q=tail->next;
}
else if(!q->num)
{
tail->next=q->next;
free(q);
q=tail->next;
}
else
{
tail=tail->next;
q=q->next;
}
}
p=p->next;
}
}
void Qsort(struct node *root)
{
struct node *p,*q;
int t;
p=root->next;
while(p)
{
q=p->next;
while(q)
{
if(p->x<q->x)
{
t=p->x;
p->x=q->x;
q->x=t;
t=p->y;
p->y=q->y;
q->y=t;
t=p->num;
p->num=q->num;
q->num=t;
}
else if(p->x==q->x&&p->x)
{
if(!q->y&&p->y)
{
t=p->x;
p->x=q->x;
q->x=t;
t=p->y;
p->y=q->y;
q->y=t;
t=p->num;
p->num=q->num;
q->num=t;
}
else if(p->y<q->y&&p->y)
{
t=p->x;
p->x=q->x;
q->x=t;
t=p->y;
p->y=q->y;
q->y=t;
t=p->num;
p->num=q->num;
q->num=t;
}
}
else if(p->x==q->x&&!p->x)
{
if(p->y<q->y)
{
t=p->x;
p->x=q->x;
q->x=t;
t=p->y;
p->y=q->y;
q->y=t;
t=p->num;
p->num=q->num;
q->num=t;
}
}
q=q->next;
}
p=p->next;
}
}
void put(struct node*root)
{
struct node *p;
p=root->next;
while(p)
{
if(p!=root->next)
printf("+");
if(p->num)
{
if(p->num!=1||(!p->x&&!p->y))
printf("%d",p->num);
}
else
{
printf("0");
p=p->next;
break;
}
if(p->x)
{
printf("x");
if(p->x!=1)
printf("^%d",p->x);
}
if(p->y)
{
printf("y");
if(p->y!=1)
printf("^%d",p->y);
}
p=p->next;
}
printf("\n");
}
int main()
{
int i,n;
char ch[5];
while(scanf("%d",&n)&&n)
{
scanf("%s",ch);
for(i=0; i<=n; i++)
head[i].next=NULL;
for(i=1; i<=n; i++)
{
scanf("%s",st);
build(&head[i]);
}
if(ch[0]=='+')
{
struct node *p;
p=&head[0];
for(i=1; i<=n; i++)
{
p->next=head[i].next;
while(p->next)
p=p->next;
}
}
if(ch[0]=='*')
{
head[0].next=head[1].next;
for(i=2; i<=n; i++)
link(&head[0],&head[i]);
}
bing(&head[0]);
Qsort(&head[0]);
put(&head[0]);
}
}