3-04. 一元多项式的乘法与加法运算(20)
时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
设计函数分别求两个一元多项式的乘积与和。
输入格式说明:
输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。
输出格式说明:
输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。
样例输入与输出:
序号 | 输入 | 输出 |
1 |
4 3 4 -5 2 6 1 -2 0 3 5 20 -7 4 3 1 |
15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 1 5 20 -4 4 -5 2 9 1 -2 0 |
2 |
2 1 2 1 0 2 1 2 -1 0 |
1 4 -1 0 2 2 |
3 |
2 -1000 1000 1000 0 2 1000 1000 -1000 0 |
-1000000 2000 2000000 1000 -1000000 0 0 0 |
4 |
0 1 999 1000 |
0 0 999 1000 |
注:提交之后的情况:未找出问题
#include<stdio.h> #include<stdlib.h> #include <malloc.h> typedef struct Node { int coef; int index; int length; struct Node * Next; }node,*Link; //创建空多项式 node * create(node *head) { head=(node *)malloc(sizeof(node)); head->Next=NULL; head->length=0; return head; } //输入有N个项的多项式 void insert(node *head,int N) { struct Node *tail,*pthis; tail=head;pthis=head; int coef,index; if(N!=0) { while(N--) { scanf("%d %d",&coef,&index); tail=(node *)malloc(sizeof(node)); tail->coef=coef; tail->index=index; tail->Next=pthis->Next; pthis->Next=tail; pthis=tail; head->length++; } } else{ tail=(node *)malloc(sizeof(node)); tail->coef=0; tail->index=0; tail->Next=pthis->Next; pthis->Next=tail; pthis=tail; } } //遍历多项式 ,不输出多余的0项 void ergodicLink(node *head) { struct Node *pthis; pthis=head->Next; int k=0;//计数用,如果多项式是0多项式,k=0; while(pthis!=NULL) { if(pthis->coef==0){ if(pthis->Next==NULL&&k!=0)printf(" "); pthis=pthis->Next; } else{ printf("%d %d",pthis->coef,pthis->index); if(pthis->Next==NULL)printf(" "); else printf(" "); pthis=pthis->Next; k++;//不是空多项式k!=0 } } if(k==0)printf("0 0 "); } //多项式相加 node *addLink(node* head,node *head1) { //新链表,存相加结果 node *addNew; addNew=create(addNew); node* pp; pp=addNew; //遍历两个链表 node *pthis,*pthat; pthis=head->Next;pthat=head1->Next; while(pthis!=NULL&&pthat!=NULL) { if(pthis->index>pthat->index)//pthis的项大就把它复制给新表 { node* newPoint=(node*)malloc(sizeof(node)); newPoint->coef=pthis->coef; newPoint->index=pthis->index; newPoint->Next=pp->Next; pp->Next=newPoint; pp=newPoint; pthis=pthis->Next; } if(pthis->index<pthat->index)//pthat的项大就把它复制给新表 { node* newPoint=(node*)malloc(sizeof(node)); newPoint->coef=pthat->coef; newPoint->index=pthat->index; newPoint->Next=pp->Next; pp->Next=newPoint; pp=newPoint; pthat=pthat->Next; } if(pthis->index==pthat->index)//一样大就相加再复制给新表 { node* newPoint=(node*)malloc(sizeof(node)); newPoint->coef=pthat->coef+pthis->coef; newPoint->index=pthat->index; newPoint->Next=pp->Next; pp->Next=newPoint; pp=newPoint; pthat=pthat->Next; pthis=pthis->Next; } } //如果pthis遍历完pthat还没遍历完,把pthat剩余加在新表上 if(pthis==NULL&&pthat!=NULL) { pp->Next=pthat; } //如果pthat遍历完pthis还没遍历完,把pthis剩余加在新表上 if(pthis!=NULL&&pthat==NULL) { pp->Next=pthis; } return addNew; } //多项式相乘 node *multiLink(node* head,node *head1) { //新链表,存相乘结果 node *addNew; addNew=create(addNew); node* pp; pp=addNew; //遍历两个链表 node *pthis,*pthat; pthis=head->Next;pthat=head1->Next; while(pthis!=NULL) { while(pthat!=NULL) { node* newPoint=(node*)malloc(sizeof(node)); newPoint->coef=pthis->coef*pthat->coef; newPoint->index=pthis->index+pthat->index; newPoint->Next=pp->Next; pp->Next=newPoint; pp=newPoint; pthat=pthat->Next; addNew->length++;//新链表长度 } pthis=pthis->Next; pthat=head1->Next; } //冒泡排序 for(pthis=addNew->Next;pthis!=NULL;pthis=pthis->Next) { for(pthat=pthis;pthat!=NULL;pthat=pthat->Next) { if(pthis->index<pthat->index) { int temp=pthis->coef; pthis->coef=pthat->coef; pthat->coef=temp; temp=pthis->index; pthis->index=pthat->index; pthat->index=temp; } } } //相同指数项相加 pthis=addNew->Next; while(pthis!=NULL) { pthat=pthis->Next; if(pthat!=NULL) { if(pthis->index==pthat->index) { pthis->coef=pthis->coef+pthat->coef; pthis->Next=pthat->Next; } else { pthis=pthis->Next; } } else pthis=pthis->Next; } return addNew; } //删除多项式 void delLink(node *head) { struct Node *pthis; pthis=head; while(pthis!=NULL) { free(pthis); head=head->Next; pthis=head; } } int main() { int N; //第一个链表 scanf("%d",&N); struct Node *head; head=create(head); insert(head,N); //ergodicLink(head); //第二个链表 scanf("%d",&N); struct Node *head1; head1=create(head1); insert(head1,N); //ergodicLink(head1); if(head->length==0&&head1->length!=0){ printf("0 0 "); ergodicLink(head1); delLink(head);delLink(head1); } else if(head->length!=0&&head1->length==0) { printf("0 0 "); ergodicLink(head); delLink(head);delLink(head1); } else if(head->length==0&&head1->length==0) { printf("0 0 "); printf("0 0 "); delLink(head);delLink(head1); } else{ //相乘 node* hehe=multiLink(head,head1); ergodicLink(hehe); //相加 node* hehe1=addLink(head,head1); ergodicLink(hehe1); delLink(head);delLink(head1);delLink(hehe1);delLink(hehe); } return 0; }