opj 多项式加法
2017-09-06 08:32
253 查看
将两个多项式按系数相加。
具体题目:多项式加法
我的做法是利用一个结点类与一个仅封装了两种操作的多项式类来实现。需要注意输出格式。具体代码如下:
#include<iostream>
#include<queue>
#include<stack>
#include<cmath>
#include<functional>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
class Node{
public:
int coeff,power;
Node * next;
Node(int c,int p,Node* n=NULL){
coeff=c;
power=p;
next=n;
}
Node(Node *n){
next=n;
}
};
class Poly{
public:
Node * head,* tail;
Poly(){
head=tail=new Node(NULL);
}
void AddPair(int c,int po);
void Addnew(int c,int po);
~Poly(){
Node *p;
while(head!=NULL){
p=head;
head=head->next;
delete p;
}
}
};
void Poly::AddPair(int c, int po){
Node *p=new Node(c,po);
tail->next=p;
tail=p;
}
void Poly::Addnew(int c, int po){
Node *p=head;
while(p->next!=NULL && p->next->power>po){
p=p->next;
}
if(p->next!=NULL && p->next->power==po){
p->next->coeff+=c;
return;
}
Node *q=new Node(c,po,p->next);
p->next=q;
if(p==tail)
tail=q;
return;
}
int main(){
int n;
cin>>n;
while(n--){
int co,po;
Poly p1,p2,p3;
while(cin>>co>>po && po>=0){
if(co!=0) p1.Addnew(co, po);
}
while(cin>>co>>po && po>=0){
if(co!=0) p2.Addnew(co, po);
}
Node * a, *b;
a=p1.head->next,b=p2.head->next;
/* while(a!=NULL){
cout<<a->coeff<<" "<<a->power<<" /// ";
a=a->next;
}
cout<<endl<<endl;
while(b!=NULL){
cout<<b->coeff<<" "<<b->power<<" /// ";
b=b->next;
} */
while(a!=NULL || b!=NULL){
if(a==NULL){
if(b==NULL) break;
else{
p3.AddPair(b->coeff, b->power);
b=b->next;
continue;
}
}
if(b==NULL){
if(a==NULL) break;
else{
p3.AddPair(a->coeff, a->power);
a=a->next;
continue;
}
}
if(a->power>b->power){
p3.AddPair(a->coeff, a->power);
a=a->next;
continue;
}
if(a->power==b->power){
p3.AddPair(a->coeff+b->coeff, a->power);
a=a->next;
b=b->next;
continue;
}
if(a->power<b->power){
p3.AddPair(b->coeff, b->power);
b=b->next;
continue;
}
}//while for a & b
Node* p=p3.head->next;
p=p3.head->next;
while(p->next!=NULL){
if(p->coeff==0) {p=p->next;continue;}
printf("[ %d %d ] ",p->coeff,p->power);
p=p->next;
}
if(p->coeff!=0)printf("[ %d %d ]",p->coeff,p->power);
if(n!=0) printf("\n");
}//while for n
}
具体题目:多项式加法
我的做法是利用一个结点类与一个仅封装了两种操作的多项式类来实现。需要注意输出格式。具体代码如下:
#include<iostream>
#include<queue>
#include<stack>
#include<cmath>
#include<functional>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
class Node{
public:
int coeff,power;
Node * next;
Node(int c,int p,Node* n=NULL){
coeff=c;
power=p;
next=n;
}
Node(Node *n){
next=n;
}
};
class Poly{
public:
Node * head,* tail;
Poly(){
head=tail=new Node(NULL);
}
void AddPair(int c,int po);
void Addnew(int c,int po);
~Poly(){
Node *p;
while(head!=NULL){
p=head;
head=head->next;
delete p;
}
}
};
void Poly::AddPair(int c, int po){
Node *p=new Node(c,po);
tail->next=p;
tail=p;
}
void Poly::Addnew(int c, int po){
Node *p=head;
while(p->next!=NULL && p->next->power>po){
p=p->next;
}
if(p->next!=NULL && p->next->power==po){
p->next->coeff+=c;
return;
}
Node *q=new Node(c,po,p->next);
p->next=q;
if(p==tail)
tail=q;
return;
}
int main(){
int n;
cin>>n;
while(n--){
int co,po;
Poly p1,p2,p3;
while(cin>>co>>po && po>=0){
if(co!=0) p1.Addnew(co, po);
}
while(cin>>co>>po && po>=0){
if(co!=0) p2.Addnew(co, po);
}
Node * a, *b;
a=p1.head->next,b=p2.head->next;
/* while(a!=NULL){
cout<<a->coeff<<" "<<a->power<<" /// ";
a=a->next;
}
cout<<endl<<endl;
while(b!=NULL){
cout<<b->coeff<<" "<<b->power<<" /// ";
b=b->next;
} */
while(a!=NULL || b!=NULL){
if(a==NULL){
if(b==NULL) break;
else{
p3.AddPair(b->coeff, b->power);
b=b->next;
continue;
}
}
if(b==NULL){
if(a==NULL) break;
else{
p3.AddPair(a->coeff, a->power);
a=a->next;
continue;
}
}
if(a->power>b->power){
p3.AddPair(a->coeff, a->power);
a=a->next;
continue;
}
if(a->power==b->power){
p3.AddPair(a->coeff+b->coeff, a->power);
a=a->next;
b=b->next;
continue;
}
if(a->power<b->power){
p3.AddPair(b->coeff, b->power);
b=b->next;
continue;
}
}//while for a & b
Node* p=p3.head->next;
p=p3.head->next;
while(p->next!=NULL){
if(p->coeff==0) {p=p->next;continue;}
printf("[ %d %d ] ",p->coeff,p->power);
p=p->next;
}
if(p->coeff!=0)printf("[ %d %d ]",p->coeff,p->power);
if(n!=0) printf("\n");
}//while for n
}
相关文章推荐
- 贪心----书架
- OPJ---7830:求小数的某一位
- 蛇形填充数组
- 逆波兰表达式(基本算法之递归和自调用函数)
- poj 前缀中的周期
- opengl es
- Mac开发必备工具(三)—— Fish shell
- Mac开发必备工具(三)—— Fish shell
- Weave 网络结构分析 - 每天5分钟玩转 Docker 容器技术(64)
- Weave 网络结构分析 - 每天5分钟玩转 Docker 容器技术(64)
- Weave 网络结构分析 - 每天5分钟玩转 Docker 容器技术(64)
- 【深圳】掌通宝科技有限公司技术总监(兼架构师),约吗
- OnPropertyChanged的使用
- Linux strace
- mutableCopy 与 copy
- 升级log4j2,tomcat--7.0.16 启动就OOM,蛋疼的问题
- Tomcat在阿里云上启动慢的解决办法
- linux自学笔记--bash特性
- 家里没有交换机没有网只能开手机wifi的情况下crt怎么连接linux虚拟机
- linux快捷方式