在PTA上刷DS的题目,有些问题和细节,放上来和大家分享和讨论
设计函数分别求两个一元多项式的乘积与和。
输入格式:
输入分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
解答
提交时间 | 状态 | 分数 | 题目编译器 | 耗时 | 用户
2017/10/15 22:52:56 | 答案正确 | 20 | C (gcc) | 2 ms | 王小C
测试点 提示 结果 耗时 内存
0 sample换个数字 答案正确 2 ms 128KB
1 同类项合并时有抵消 答案正确 2 ms 256KB
2 系数和指数取上限,结果有零多项式 答案正确 2 ms 128KB
3 输入有零多项式和常数多项式 答案正确 2 ms 128KB
#include <stdio.h>
#include <stdlib.h>
#ifndef NULL
#define NULL 0
#endif // NULL
typedef struct node *PtrToNode;
struct node
{
int coef;
int expon;
struct node *Next;
};
typedef PtrToNode List;
List createpoly();//create polynomial according to the data from console
List multpoly(List,List);//multiple of two polynomials
void printpoly(List P);//print out a polynomial
List Addpoly(List,List);//the sum up of two polynomial
void Attach(int,int,List *P);//attach a new node to the polynomial
void InsertPoly(int,int,List PP);//insert a new node into a polynomial
int main()
{
List P1,P2,PP,PS;
P1 = createpoly();
P2 = createpoly();
PP = multpoly(P1,P2);
printpoly(PP);
PS = Addpoly(P1,P2);
printpoly(PS);
return 0;
}
List createpoly()
{
List P,ptr,*rear=NULL;
int c,e;
int N;
P = (List)malloc(sizeof(struct node));
P->Next = NULL;
ptr = P;
rear = &ptr;
scanf("%d",&N);
while(N--)
{
scanf(" %d %d",&c,&e);
Attach(c,e,rear); //不确定这里用的应该是指针还是prt
}
ptr = P;
P = P->Next;
free(ptr);
return(P);
}
void Attach(int c,int e,List *prear)
{
List P;
P = (List)malloc(sizeof(struct node ));
P->coef = c;
P->expon = e;
P->Next = NULL;
(*prear)->Next = P;
*prear = P;
}
List Addpoly(List P1,List P2)
{
List PP,ptr,t1,t2;
PP = (List)malloc(sizeof(struct node));
PP->Next = NULL;
ptr = PP;
if(!P1&&!P2)
return NULL;
t1 = P1;t2 = P2;
while(t1&&t2)
{
if(t1->expon > t2->expon)
{
Attach(t1->coef,t1->expon,&ptr);
t1 = t1->Next;
}
else if(t2->expon > t1->expon)
{
Attach(t2->coef,t2->expon,&ptr);
t2 = t2->Next;
}
else
{
if(t1->coef + t2->coef == 0)
{
t1 = t1->Next;
t2 = t2->Next;
}
else{
Attach(t1->coef+t2->coef,t1->expon,&ptr);
t1 = t1->Next;
t2 = t2->Next;
}
}
}
while(t1)
{
Attach(t1->coef,t1->expon,&ptr);
t1 = t1->Next;
}
while(t2)
{
Attach(t2->coef,t2->expon,&ptr);
t2 = t2->Next;
}
ptr = PP;
PP = PP->Next;
free(ptr);
return PP;
}
List multpoly(List P1,List P2)
{
List PP,ptr,*prear=NULL,t1,t2;
if(P1 == NULL || P2 == NULL)
return NULL;
PP = (List)malloc(sizeof(struct node));
PP->Next = NULL;
ptr = PP;
prear = &ptr;
t1 = P1;t2 = P2;
while(t2)
{
Attach(t1->coef*t2->coef,t1->expon+t2->expon,prear);
t2 = t2->Next;
}
t1 = t1->Next;
while(t1)
{
t2 = P2;
while(t2)
{
InsertPoly(t1->coef*t2->coef,t1->expon+t2->expon,PP->Next);
t2 = t2->Next;
}
t1 = t1->Next;
}
ptr = PP;
PP = PP->Next;
free(ptr);
return PP;
}
void InsertPoly(int c,int e,List PP)
{
List ptr,P,tempP;
if(PP->expon < e)
{
P = (List)malloc(sizeof(struct node));
P->coef = c;
P->expon = e;
P->Next = PP;
PP = P;
}
else
{
ptr = PP;
while(ptr->Next)
{
if(ptr->Next->expon > e)
ptr = ptr->Next;
else
break;
}
if(ptr->Next == NULL)
{
P = (List)malloc(sizeof(struct node));
P->coef = c;
P->expon = e;
P->Next = NULL;
ptr->Next = P;
}
else if(ptr->Next->expon < e)
{
P = (List)malloc(sizeof(struct node));
P->coef = c;
P->expon = e;
P->Next = ptr->Next;
ptr->Next = P;
}
else
{
ptr->Next->coef += c;
if(ptr->Next->coef == 0)
{
tempP = ptr->Next;
ptr->Next = tempP->Next;
free(tempP);
}
}
}
}
void printpoly(List P)
{
int flag = 0;
List ptr = P;
if(!P)
{
printf("0 0\n");
return ;
}
while(ptr)
{
if(!flag)
{
printf("%d %d",ptr->coef,ptr->expon);
flag = 1;
ptr = ptr->Next;
}
else{
printf(" %d %d",ptr->coef,ptr->expon);
ptr = ptr->Next;
}
}
printf("\n");
}
网友评论