链表应用——二元多项式

二元多项式

Time Limit: 1000MS Memory Limit: 65536KB
Problem Description
 

给你多个二元多项式和一个操作符,让你输出操作符操作这些二元多项式之后的结果。

Input
 

首先输入二元多项式的个数n和操作符号(‘+’,‘*’);

后面n行输入每一个多项式。

多组输入,当n=0的时候结束输入。

(n<5,二元多项式的长度小于1000,二元多项式都是由x,y,^,数字,’+’组成的)

Output
 

输出操作之后的结果。

(输出的顺序按照:x^2>x>xy^2>xy>y^2>y>常数)

Example Input
2 +
3x+4y^2+3xy+6x^10y^2+1
2x+6y
0
Example Output
6x^10y^2+5x+3xy+4y^2+6y+1
Hint
 
Author
 zp    

转载代码:
#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=new 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]);
    }
    return 0;
}

出处: http://blog.csdn.net/qq_33435265/article/details/51959208
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值