二元多项式

来源:互联网 发布:淘宝兼职广告语大全集 编辑:程序博客网 时间:2024/04/28 04:58
//参考答案1#include <stdio.h>#include <string.h>#include <vector>#include <algorithm>using namespace std;struct node{int mod, exp_x, exp_y;};bool zero;int mod, exp_x, exp_y;vector<struct node>poly[110];int cmp(const struct node a, const struct node b){if(a.exp_x == 0 && a.exp_y == 0)return 0;if(b.exp_x == 0 && b.exp_y == 0)return 1;if(a.exp_x != b.exp_x)return a.exp_x > b.exp_x;else if(a.exp_y == 0)return 1;else if(b.exp_y == 0)return 0;return a.exp_y > b.exp_y;}void setmod(char s[]){if(sscanf(s, "%d", &mod) != 1)mod = 1;}void setexp(char s[]){exp_x = exp_y = 0;int i = 0;while(s[i] >= '0' && s[i] <= '9')i++;if(s[i] == 'x'){if(s[++i] == '^')sscanf(&s[++i], "%d", &exp_x);else exp_x = 1;}while(s[i] >= '0' && s[i] <= '9')i++;if(s[i] == 'y'){if(s[++i] == '^')sscanf(&s[++i], "%d", &exp_y);else exp_y = 1;}}int main(){int n;char op[5];char str[1010], tmp[1010];while(scanf("%d", &n) != EOF && n){scanf("%s", op);zero = 0;for(int i = 0; i <= 100; i++)poly[i].clear();for(int i = 1; i <= n; i++){scanf("%s", str);if(strcmp(str, "0") == 0)zero = 1;int j = 0;while(1){int k = 0;while(str[j] != '+' && str[j] != '\0')tmp[k++] = str[j++];tmp[k] = 0;setmod(tmp);setexp(tmp);poly[i].push_back((struct node){mod, exp_x, exp_y});if(str[j] == '\0')break;else j++;}}if(op[0] == '+'){for(int i = 1; i <= n; i++)for(vector<struct node>::iterator j = poly[i].begin(); j != poly[i].end(); j++){int findit = 0;for(vector<struct node>::iterator k = poly[0].begin(); k != poly[0].end(); k++)if(j->exp_x == k->exp_x && j->exp_y == k->exp_y){k->mod += j->mod;findit = 1;break;}if(findit == 0)poly[0].push_back(*j);}}else if(op[0] == '*'){int rear = n+1, l = 1;while(l < rear-1){for(vector<struct node>::iterator i = poly[l].begin(); i != poly[l].end(); i++){for(vector<struct node>::iterator j = poly[l+1].begin(); j != poly[l+1].end(); j++)poly[rear].push_back((struct node){i->mod*j->mod, i->exp_x+j->exp_x, i->exp_y+j->exp_y});}l += 2;rear++;}for(vector<struct node>::iterator i = poly[rear-1].begin(); i != poly[rear-1].end(); i++)poly[0].push_back(*i);//mergefor(vector<struct node>::iterator i = poly[0].begin(); i != poly[0].end(); i++){for(vector<struct node>::iterator j = i+1; j != poly[0].end(); j++)if(j->exp_x == i->exp_x && j->exp_y == i->exp_y){i->mod += j->mod;poly[0].erase(j);}}}if(zero == 1 && op[0] == '*'){printf("0\n");}else{sort(poly[0].begin(), poly[0].end(), cmp);bool first = 1;for(vector<struct node>::iterator k = poly[0].begin(); k != poly[0].end(); k++){if(k->mod == 0)continue;if(!first)printf("+");else first = 0;if(k->mod != 1)printf("%d", k->mod);else if(k->exp_x == 0 && k->exp_y == 0)printf("%d", k->mod);if(k->exp_x != 0){if(k->exp_x == 1)printf("x");else printf("x^%d", k->exp_x);}if(k->exp_y != 0){if(k->exp_y == 1)printf("y");else printf("y^%d", k->exp_y);}}printf("\n");}}return 0;} //参考答案2#include<stdio.h>#include<string.h>#include<stdlib.h>struct node{    int x;    int y;    int num;    struct node *next;} head[10];char st[1010];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!=NULL)    {        q=root->next;        while(q!=NULL)        {            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!=NULL)    {        tail=p;        q=tail->next;        while(q!=NULL)        {            if(p->x==q->x&&p->y==q->y&&p->num&&q->num)            {                p->num+=q->num;                tail->next=q->next;                free(q);                q=tail->next;            }            else if(p->num==0&&q->num==0)            {                p->x=0;                p->y=0;                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!=NULL)    {        q=p->next;        while(q!=NULL)        {            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==0&&p->y!=0)                {                    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!=0)                {                    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==0)            {                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!=NULL)    {        if(p!=root->next)            printf("+");        if(p->num)        {            if(p->num!=1||(p->x==0&&p->y==0))                printf("%d",p->num);        }        else        {            printf("0");            p=p->next;            continue;        }        if(p->x!=0)        {            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!=0)    {        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!=NULL)                    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]);    }}

0 0
原创粉丝点击