noj 1005 多项式加法
来源:互联网 发布:数据挖掘毕业论文题目 编辑:程序博客网 时间:2024/04/30 06:57
对于输入,由于是按指数递减输入的,故我们只需按照输入顺序进行存储即可。
对待一个输入:
1.如果当前多项式为空,那么将此项放入第一项
2.如果当前多项式不为空
3.如果有指数相同的项,那么将此项与指数相同的项合并,如果系数变为0,则将此项删除。
4.如果没有指数相同的项(那么此项的指数一定比当前所有项的指数都小),那么将此项放入多项式末尾。
这样就可以得到输入的两个多项式,并输出。
对于最后一步,多项式加法,即将多项式指数相同的项分别相加,而将指数不同的项保留输出即是加法结果。
做了两天零一夜才过的,基础太不好了!!!还是要细心,多刷题,加油!
#include<iostream> #include<stdio.h> #include<malloc.h> using namespace std; struct node { int x,z; node *next; node(int a,int b):x(a),z(b),next(NULL){} };void print(node *head){ node *q=head->next; if(q==NULL) printf("0"); else { int f=1,f2=0; while(q) { if(q==head->next) { if(q->x!=1) { if(q->x==-1) printf("-"); else printf("%d",q->x); } } else { if(q->x>0) { printf("+"); if(q->x!=1) printf("%d",q->x); } else { if(q->x!=-1) printf("%d",q->x); else printf("-"); } } if(q->z!=0) printf("X"); else { if(q->x==1||q->x==-1) printf("1"); q=q->next; continue; } if(q->z!=1) printf("^%d",q->z); q=q->next; } } printf("\n");} int main() { node *tail,*tail1,*tmp,*hea,*hea1; int n,m,f=1; tail=new node(0,0); while(scanf("%d%d",&n,&m)&&(n!=0||m!=-1)) { if(n==0) continue; tmp=new node(n,m); if(tail->z==tmp->z) tail->x=tail->x+tmp->x; else { tail->next=tmp; tail=tmp; } if(f) { hea=new node(0,0); tmp->next=hea->next; hea->next=tmp; } f=0; } print(hea); f=1; tail1=new node(0,0); while(scanf("%d%d",&n,&m)&&(n!=0||m!=-1)) { if(n==0) continue; node *p=hea,*q=hea->next,*cur,*tmp1; int f1=0; cur=new node(n,m); tmp1=new node(n,m); if(tail1->z==tmp1->z) tail1->x=tail1->x+tmp1->x; else { tail1->next=tmp1; tail1=tmp1; } if(f) { hea1=new node(0,0); tmp1->next=hea1->next; hea1->next=tmp1; } while(q) { if(q->z==cur->z) { f1=1; q->x=cur->x+q->x; if(q->x==0) { p->next=q->next; delete q; q=p->next; } break; } if(q->z<cur->z) { f1=1; p->next=cur; cur->next=q; break; } p=p->next; q=p->next; } if(!f1) { p->next=cur; cur->next=NULL; } f=0; } print(hea1); print(hea); return 0; }
0 0
- noj 1005 多项式加法
- noj 1005 多项式加法
- 1005——多项式加法
- 南邮 OJ 1005 多项式加法
- 南邮OJ 1005 多项式加法
- 多项式加法
- 多项式加法
- 多项式加法
- 多项式加法
- 多项式加法
- 多项式加法
- 多项式加法
- 多项式加法
- 多项式加法
- 多项式加法
- 多项式加法
- 多项式加法
- 多项式加法
- poj2686(Traveling by Stagecoach)状态压缩dp+DAG
- 1. Two Sum(c++实现)
- Java中byte与16进制字符串的互相转换
- 详解JAVA高质量代码之数组与集合
- iOS-UIGestureRecognizer(手势)
- noj 1005 多项式加法
- MyEclipse10破解问题
- jenkins自动化部署
- Padding +Margin+gravity
- [LeetCode] 64. Minimum Path Sum
- ios developer tiny share-20160727
- 重学Statistics, Cha14 Simple Linear Regression
- 什么是进程,什么是线程
- 服务器端架构及实战 — C#分享