pat--一元多项式的乘法与加法

来源:互联网 发布:我的世界ip地址和端口 编辑:程序博客网 时间:2024/05/17 07:46
5-8 一元多项式的乘法与加法运算   (20分)

设计函数分别求两个一元多项式的乘积与和。

输入格式:

输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。

输出格式:

输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0

输入样例:

4 3 4 -5 2  6 1  -2 03 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 15 20 -4 4 -5 2 9 1 -2 0

测试点:

序号
输入
输出
1
4 3 4 -5 2  6 1  -2 03 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 15 20 -4 4 -5 2 9 1 -2 0

2
2 1 2 1 02 1 2 -1 0

1 4 -1 02 2

3
2 -1000 1000 1000 02 1000 1000 -1000 0

-1000000 2000 2000000 1000 -1000000 00 0

4
01 999 1000

0 0999 1000




一元多项式主要考察链表的应用,其中这里需要注意几个问题
首先注意  某2个项如果指数相等、系数互为相反数,他们相加后输出直接就没了,而不是输出 0 y(y是指数)  。
然后注意,如果2个链表里的项都是对应的2对 指数相等、系数互为相反数 的项,这时候输出应该是0 0 
乘法时注意是按降序排列,这里我用了一个临时链表来存储第一个链表的一个项与第二个链表的每一项相乘的结果,然后把这个结果与储存结果的链表相加,最后就是有序的(因为我们做加法运算时会使结果有序)

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>using namespace std;typedef struct Linklist{          ///定义链表int x;int y;struct Linklist *next;}listnode,*list;void init(list& l){               ///初始化链表,为链表分配空间,注意,因为我们用的void型,所以这里必须传引用,否则没有用l = (list)malloc(sizeof(listnode));l->x = 0;l->y = 0;l->next = NULL;  }void printlist(list l){            ///打印一个链表listnode *p,*t,*q;list tmp;                 ///因为最后要筛选系数是 0 的项,所以我把筛选后的结果存储在了这个临时链表,同时也是为了init(tmp);                ///避免出现第二个注意的情况p = l -> next;q = tmp;if(!p) printf("0 0\n");    ///链表为空,就打印0 0else{t = (list)malloc(sizeof(listnode));if(p->x != 0){t->x = p->x;t->y = p->y;t->next = NULL;q->next = t;q = t;}p = p->next;if(p){while(p){t = (list)malloc(sizeof(listnode));if(p->x != 0){     //这个if就是为了筛选系数为 0 的项    t->x = p->x;            t->y = p->y;            t->next = NULL;            q->next = t;            q = t;}p = p->next;}}listnode *tt = tmp->next;         ///打印最后的链表if(!tt) printf("0 0\n");else{printf("%d %d",tt->x,tt->y);tt = tt->next;if(!tt) printf("\n");else{while(tt){printf(" %d %d",tt->x,tt->y);tt = tt->next;}printf("\n");}    }}}void input(list& l,int size){            //输入数据,构建链表listnode *p,*q;q = l;for(int i = 0; i < size; ++i){int tx,ty;scanf("%d%d",&tx,&ty);p = (list)malloc(sizeof(listnode));p->x = tx;p->y = ty;p->next = NULL;q->next = p;q = p;}}void add(list la,list lb,list& l){     //2个链表的加法listnode *p1,*p2,*p,*q;p1 = la->next;p2 = lb->next;q = l;while(p1&&p2){p = (list)malloc(sizeof(listnode));if(p1->y == p2->y){p->x = p1->x + p2->x;p->y = p1->y;p->next = NULL;q->next = p;q = p;p1 = p1->next;p2 = p2->next;}else if(p1->y > p2->y){p->x = p1->x;p->y = p1->y;p->next = NULL;q->next = p;q = p;p1 = p1->next; }else{p->x = p2->x;p->y = p2->y;p->next = NULL;q->next = p;q = p;p2 = p2->next; }}while(p1){p = (list)malloc(sizeof(listnode));p->x = p1->x;p->y = p1->y;p->next = NULL;q->next = p;q = p;p1 = p1->next;}while(p2){p = (list)malloc(sizeof(listnode));p->x = p2->x;p->y = p2->y;p->next = NULL;q->next = p;q = p;p2 = p2->next;}}void mul(list la,list lb,list& l){     //2个链表的乘法listnode *p1,*p2,*p,*q;list tmp;                //临时链表,用来存储第一个链表的一个项与第二个链表的每一项相乘的结果 init(tmp);p1 = la->next;p2 = lb->next;if(!p1 || !p2) return;while(p1){init(tmp);q = tmp;p2 = lb->next;while(p2){p =  (list)malloc(sizeof(listnode));p->x = p1->x * p2->x;p->y = p1->y + p2->y;p->next = NULL;q->next = p;q = p;p2 = p2->next;}add(l,tmp,l);p1 = p1->next;}}int main(int argc, char *argv[]) {list l1,l2;list addl,mull;   //一个存储加法后的结果,一个存储乘法后的结果int n,m;scanf("%d",&n);init(l1);init(l2);init(addl);init(mull);input(l1,n);scanf("%d",&m);input(l2,m);mul(l1,l2,mull);printlist(mull);add(l1,l2,addl);printlist(addl);return 0;}



1 0
原创粉丝点击