马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
求告知求乘积出错原因 谢谢!
设计函数分别求两个一元多项式的乘积与和。
输入格式:
输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。
输出格式:
输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0。
输入样例:
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
我的代码:#include <stdio.h>
#include <stdlib.h>
typedef struct Node *node;
struct Node
{
int x;
int z;
node next;
};
node read();
void print(node lsit);
node he(node list1,node list2);
node ji(node list1,node list2);
node read()
{
int i;
scanf("%d",&i);
node head = (node)malloc(sizeof(node));
head->next = NULL;
node p = head;
while(i--)
{
node temp = (node)malloc(sizeof(node));
scanf("%d %d",&temp->x,&temp->z);
p->next = temp;
p = p->next;
}
p->next = NULL;
return head;
}
node he(node list1,node list2)
{
node temp;
node head = (node)malloc(sizeof(node));
head->next = NULL;
node p = head;
if(list2->next == NULL)
return list1;
else
{
list1 = list1->next;
list2 = list2->next;
while(list1 && list2)
{
temp = (node)malloc(sizeof(node));
if(list1->z == list2->z)
{
temp->x = list1->x + list2->x;
temp->z = list1->z;
list1 = list1->next;
list2 = list2->next;
}
else if(list1->z > list2->z)
{
temp->x = list1->x;
temp->z = list1->z;
list1 = list1->next;
}
else
{
temp->x = list2->x;
temp->z = list2->z;
list2 = list2->next;
}
p->next = temp;
p = p->next;
}
for(;list1;list1 = list1->next)temp = (node)malloc(sizeof(node)),temp->x = list1->x,temp->z = list1->z,p->next = temp,p = p->next;
for(;list2;list2 = list2->next)temp = (node)malloc(sizeof(node)),temp->x = list2->x,temp->z = list2->z,p->next = temp,p = p->next;
p->next = NULL;
return head;
}
}
node ji(node list1,node list2)
{
node t;
node OK = (node)malloc(sizeof(node));
OK->next = NULL;
node L = OK;
list1 = list1->next;
list2 = list2->next;
for(list1;list1;list1=list1->next)
{
node temp;
node head = (node)malloc(sizeof(node));
head->next = NULL;
node p = head;
for(t = list2;t;t=t->next)
{
temp = (node)malloc(sizeof(node));
temp->x = list1->x * t->x;
temp->z = list1->z + t->z;
p->next = temp;
p = p->next;
}
p->next = NULL;
L = he(head,L);
}
L->next = NULL;
return OK;
}
void print(node list)
{
if(list->next)
{
list = list->next;
while(list)
{
printf("%d %d",list->x,list->z);
list = list->next;
if(list)
printf(" ");
}
}
else
printf("0 ");
}
int main()
{
node list1,list2;
list1 = read();
list2 = read();
// print(list1);
// print(list2);
print(he(list1,list2));
print(ji(list1,list2));
return 0;
}
|