学校ACM上面的题目,题目不难,不少细节。本质就是链表操作,首先是题目要求:
输入
两组数据,每一组代表一个一元整系数多项式,有多行组成,其中每一行给出多项式每一项的系数和指数,这些行按指数递减次序排序,每一组结束行为
0 -1
输出
三组数据,前两组为一元整系数多项式,最后一组为两个多项式的和。
一元整系数多项式输出形式如下:
(1)多项式项4x输出为4X
(2)多项式项4x2输出为4X^2
(3)第一项系数为正数时,加号不要输出
(4)除常系数项外,项系数为1不显式输出,-1输出为-
例如,4x3- x2+x-1正确输出形式为4X^3-X^2+X-1,错误输出形式为 +4X^3-1X^2+1X-1
样例输入
3 14
-8 8
6 2
2 0
0 -1
2 10
4 8
-6 2
0 -1
样例输出
3X^14-8X^8+6X^2+2
2X^10+4X^8-6X^2
3X^14+2X^10-4X^8+2
(输出格式要求比较麻烦,但输入的要求让建立链表变得简单)
下面是我的答案,建立、打印、加法操作、乘法操作分别写成子程序
多项式加法:
#include<iostream>
using namespace std;
typedef struct node{
int cof,exp;
struct node *next;
}Node;
void creat(Node *first){ //指数递减次序排序,所以可以插入到链表的最后
int xs,zs;
Node *p=first,*p1=p,*tmp;
while((cin>>xs>>zs)&&(xs!=0||zs!=-1)){
if(xs==0||zs==-1)continue;
tmp=new Node();
tmp->cof=xs;
tmp->exp=zs;
p->next=tmp;
tmp->next=first;
p=p->next;
}//while
}
void print(Node *first){
Node *p=first->next;
while(p->exp!=-1){
if(p==first->next){
if(p->cof!=1)
if(p->cof==-1)cout<<"-";
else if(p->cof<0)cout<<p->cof;
else cout<<p->cof;
}
else{
if(p->cof==1)cout<<"+";
else if(p->cof==-1)cout<<"-";
else if(p->cof>0)cout<<"+"<<p->cof;
else if(p->cof<0)cout<<p->cof;
}
if(p->exp==0&&(p->cof==1||p->cof==-1))cout<<1;
else if(p->exp==0){}
else if(p->exp==1)cout<<"X";
else cout<<"X^"<<p->exp;
p=p->next;
}//while
cout<<endl;
}
int exp_comp(Node *p,Node *q){
if(p->exp==q->exp)return 0;
else if(p->exp>q->exp)return 1;
else return -1;
}
void add(Node *p,Node *q){ //相加后的保存在q指向的链表中
Node *tmp,*q1=q; //q1指向q的前一个节点
p=p->next,q=q->next;
while(p->exp!=-1)
switch(exp_comp(p,q)){
case -1:q1=q,q=q->next;break; //q,q1后移,找到小于等于p指向的指数
case 0: //直接将系数相加,考虑系数为0的情况
q->cof+=p->cof;
if(q->cof==0){
q1->next=q->next;
delete(q);
q=q1->next;
p=p->next;
}
else{
p=p->next;
q1=q,q=q->next;
}
break;
case 1://p指向的exp大于q指向的exp,插到q之前
tmp=new Node();
tmp->cof=p->cof;
tmp->exp=p->exp;
q1->next=tmp;
q1=tmp;
tmp->next=q;
p=p->next;
break;
}
}
int main(){
Node *first[2];
for(int i=0;i<2;i++){
first[i]=new Node();
first[i]->cof=0;
first[i]->exp=-1;
first[i]->next=first[i]; //循环链表
creat(first[i]);
if(first[i]->next->exp!=-1)print(first[i]); //无输入则输出0
else cout<<0<<endl;
}
add(first[0],first[1]);
if(first[1]->next->exp!=-1)print(first[1]);
else cout<<0;
system("pause");
return 0;
}
多项式乘法:
#include<iostream>
using namespace std;
typedef struct node{
int cof,exp;
struct node *next;
}Node;
void multiply(Node *p,Node *q,Node *r){
if(p->next->exp==-1||q->next->exp==-1)return; //表示存在一个多项式为0
Node *t,*t1,*tmp;
p=p->next,q=q->next;
while(p->exp!=-1){ //拿p中的每一项去乘q指向的多项式
while(q->exp!=-1){ //乘积放入r中,其实是插入建立r链表的过程
t=r->next,t1=r;
tmp=new Node();
tmp->cof=p->cof*q->cof;
tmp->exp=p->exp+q->exp;
while(tmp->exp<t->exp){ //寻找插入位置
t1=t;
t=t->next;
if(t->exp==-1)break; //未找到插入位置
}
if(t->exp==-1){ //插入到最后
t1->next=tmp;
tmp->next=r;
}
else if(tmp->exp==t->exp){
t->cof+=tmp->cof;
if(t->cof==0){
t1->next=t->next;
delete(t);
t=t1->next;
}
}
else{
tmp->next=t;
t1->next=tmp;
}
q=q->next;
}
q=q->next;
p=p->next;
}
}
void creat(Node *first){
int xs,zs;
Node *p=first,*p1=p,*tmp;
while((cin>>xs>>zs)&&(xs!=0||zs!=-1)){
if(xs==0||zs==-1)continue;
tmp=new Node();
tmp->cof=xs;
tmp->exp=zs;
p->next=tmp;
tmp->next=first;
p=p->next;
}//while
}
void print(Node *first){
Node *p=first->next;
while(p->exp!=-1){
if(p==first->next){
if(p->cof!=1)
if(p->cof==-1)cout<<"-";
else if(p->cof<0)cout<<p->cof;
else cout<<p->cof;
}
else{
if(p->cof==1)cout<<"+";
else if(p->cof==-1)cout<<"-";
else if(p->cof>0)cout<<"+"<<p->cof;
else if(p->cof<0)cout<<p->cof;
}
if(p->exp==0&&(p->cof==1||p->cof==-1))cout<<1;
else if(p->exp==0){}
else if(p->exp==1)cout<<"X";
else cout<<"X^"<<p->exp;
p=p->next;
}//while
cout<<endl;
}
int main(){
Node *first[2];
for(int i=0;i<2;i++){
first[i]=new Node();
first[i]->cof=0;
first[i]->exp=-1;
first[i]->next=first[i];
creat(first[i]);
if(first[i]->next->exp!=-1)print(first[i]);
else cout<<0<<endl;
}
Node *r=new Node(); //将乘积放入r指向的链表中
r->cof=0;r->exp=-1,r->next=r;
multiply(first[0],first[1],r);
if(r->next->exp!=-1)print(r);
else cout<<0;
system("pause");
return 0;
}