二元多项式

本文介绍了一种处理二元多项式的加法和乘法运算的方法,并通过C++代码实现了解决方案。具体包括二元多项式的输入解析、运算处理及结果输出等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://blog.youkuaiyun.com/qq_33435265/article/details/51959208

二元多项式

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

提示

//这是群里的, 不是自己写的 ~~~~转载
[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include <bits/stdc++.h>  
  2. struct node  
  3. {  
  4.     int x,y,num;  
  5.     struct node *next;  
  6. } head[9];  
  7. char st[1024];  
  8. struct node *creat()  
  9. {  
  10.     struct node *p;  
  11.     p=(struct node*)malloc(sizeof(struct node));  
  12.     p->x=0,p->y=0,p->num=0;  
  13.     p->next=NULL;  
  14.     return p;  
  15. };  
  16. void build(struct node *root)  
  17. {  
  18.     int len,i,data,temp;  
  19.     struct node *p;  
  20.     len=strlen(st);  
  21.     data=0,temp=1;  
  22.     p=creat();  
  23.     for(i=len-1; i>=0; i--)  
  24.     {  
  25.         if(st[i]<='9'&&st[i]>='0')  
  26.         {  
  27.             data=(st[i]-'0')*temp+data;  
  28.             temp*=10;  
  29.         }  
  30.         if(st[i]=='x')  
  31.         {  
  32.             if(data)  
  33.                 p->x=data;  
  34.             else  
  35.                 p->x=1;  
  36.             data=0,temp=1;  
  37.         }  
  38.         if(st[i]=='y')  
  39.         {  
  40.             if(data)  
  41.                 p->y=data;  
  42.             else  
  43.                 p->y=1;  
  44.             data=0,temp=1;  
  45.         }  
  46.         if(st[i]=='+')  
  47.         {  
  48.             if(data)  
  49.                 p->num=data;  
  50.             else if(p->x||p->y)  
  51.                 p->num=1;  
  52.             data=0,temp=1;  
  53.             p->next=root->next;  
  54.             root->next=p;  
  55.             p=creat();  
  56.         }  
  57.     }  
  58.     if(data)  
  59.         p->num=data;  
  60.     else if(p->x||p->y)  
  61.         p->num=1;  
  62.     p->next=root->next;  
  63.     root->next=p;  
  64. }  
  65. void link(struct node*ROOT,struct node*root)  
  66. {  
  67.     struct node *p,*q,*head,*t;  
  68.     p=ROOT->next;  
  69.     head=creat();  
  70.     while(p)  
  71.     {  
  72.         q=root->next;  
  73.         while(q)  
  74.         {  
  75.             t=creat();  
  76.             t->num=p->num*q->num;  
  77.             t->x=p->x+q->x;  
  78.             t->y=p->y+q->y;  
  79.             t->next=head->next;  
  80.             head->next=t;  
  81.             q=q->next;  
  82.         }  
  83.         p=p->next;  
  84.     }  
  85.     ROOT->next=head->next;  
  86.     free(head);  
  87. }  
  88. void bing(struct node *root)  
  89. {  
  90.     struct node *p,*q,*tail;  
  91.     p=root->next;  
  92.     while(p)  
  93.     {  
  94.         tail=p;  
  95.         q=tail->next;  
  96.         while(q)  
  97.         {  
  98.             if(p->x==q->x&&p->y==q->y)  
  99.             {  
  100.                 p->num+=q->num;  
  101.                 tail->next=q->next;  
  102.                 free(q);  
  103.                 q=tail->next;  
  104.             }  
  105.             else if(!q->num)  
  106.             {  
  107.                 tail->next=q->next;  
  108.                 free(q);  
  109.                 q=tail->next;  
  110.             }  
  111.             else  
  112.             {  
  113.                 tail=tail->next;  
  114.                 q=q->next;  
  115.             }  
  116.         }  
  117.         p=p->next;  
  118.     }  
  119. }  
  120. void Qsort(struct node *root)  
  121. {  
  122.     struct node *p,*q;  
  123.     int t;  
  124.     p=root->next;  
  125.     while(p)  
  126.     {  
  127.         q=p->next;  
  128.         while(q)  
  129.         {  
  130.             if(p->x<q->x)  
  131.             {  
  132.                 t=p->x;  
  133.                 p->x=q->x;  
  134.                 q->x=t;  
  135.                 t=p->y;  
  136.                 p->y=q->y;  
  137.                 q->y=t;  
  138.                 t=p->num;  
  139.                 p->num=q->num;  
  140.                 q->num=t;  
  141.             }  
  142.             else if(p->x==q->x&&p->x)  
  143.             {  
  144.                 if(!q->y&&p->y)  
  145.                 {  
  146.                     t=p->x;  
  147.                     p->x=q->x;  
  148.                     q->x=t;  
  149.                     t=p->y;  
  150.                     p->y=q->y;  
  151.                     q->y=t;  
  152.                     t=p->num;  
  153.                     p->num=q->num;  
  154.                     q->num=t;  
  155.                 }  
  156.                 else if(p->y<q->y&&p->y)  
  157.                 {  
  158.                     t=p->x;  
  159.                     p->x=q->x;  
  160.                     q->x=t;  
  161.                     t=p->y;  
  162.                     p->y=q->y;  
  163.                     q->y=t;  
  164.                     t=p->num;  
  165.                     p->num=q->num;  
  166.                     q->num=t;  
  167.                 }  
  168.             }  
  169.             else if(p->x==q->x&&!p->x)  
  170.             {  
  171.                 if(p->y<q->y)  
  172.                 {  
  173.                     t=p->x;  
  174.                     p->x=q->x;  
  175.                     q->x=t;  
  176.                     t=p->y;  
  177.                     p->y=q->y;  
  178.                     q->y=t;  
  179.                     t=p->num;  
  180.                     p->num=q->num;  
  181.                     q->num=t;  
  182.                 }  
  183.             }  
  184.             q=q->next;  
  185.         }  
  186.         p=p->next;  
  187.     }  
  188. }  
  189. void put(struct node*root)  
  190. {  
  191.     struct node *p;  
  192.     p=root->next;  
  193.     while(p)  
  194.     {  
  195.         if(p!=root->next)  
  196.             printf("+");  
  197.         if(p->num)  
  198.         {  
  199.             if(p->num!=1||(!p->x&&!p->y))  
  200.                 printf("%d",p->num);  
  201.         }  
  202.         else  
  203.         {  
  204.             printf("0");  
  205.             p=p->next;  
  206.             break;  
  207.         }  
  208.         if(p->x)  
  209.         {  
  210.             printf("x");  
  211.             if(p->x!=1)  
  212.                 printf("^%d",p->x);  
  213.         }  
  214.         if(p->y)  
  215.         {  
  216.             printf("y");  
  217.             if(p->y!=1)  
  218.                 printf("^%d",p->y);  
  219.         }  
  220.         p=p->next;  
  221.     }  
  222.     printf("\n");  
  223. }  
  224. int main()  
  225. {  
  226.     int i,n;  
  227.     char ch[5];  
  228.     while(scanf("%d",&n)&&n)  
  229.     {  
  230.         scanf("%s",ch);  
  231.         for(i=0; i<=n; i++)  
  232.             head[i].next=NULL;  
  233.         for(i=1; i<=n; i++)  
  234.         {  
  235.             scanf("%s",st);  
  236.             build(&head[i]);  
  237.         }  
  238.         if(ch[0]=='+')  
  239.         {  
  240.             struct node *p;  
  241.             p=&head[0];  
  242.             for(i=1; i<=n; i++)  
  243.             {  
  244.                 p->next=head[i].next;  
  245.                 while(p->next)  
  246.                     p=p->next;  
  247.             }  
  248.         }  
  249.         if(ch[0]=='*')  
  250.         {  
  251.             head[0].next=head[1].next;  
  252.             for(i=2; i<=n; i++)  
  253.                 link(&head[0],&head[i]);  
  254.         }  
  255.         bing(&head[0]);  
  256.         Qsort(&head[0]);  
  257.         put(&head[0]);  
  258.     }  
  259. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值