分享日常
设计函数分别求两个一元多项式的乘积与和。
输入格式:
输入分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
我得仅有 18分,错在输入有零多项式和常数多项式,
#include<stdio.h>
#include<stdlib.h>
typedef struct LNode *List;
struct LNode{
int X;
int P;
struct LNode *next;
};
List make_list(int num);
void attach(int x, int p, List *ptrL);
List add_link(List P1, List P2);
List mul_link(List P1, List P2);
int compare(List P1, List P2);
void attach_link(int x, int p, List *ptrL);
void attach_for(int x, int p, List *L);
void Print_link(List PtrL);
void free_link(List PtrL);
int main()
{
int n1, n2;
List P1, P2;
List P_S, P_M;
scanf("%d", &n1);
P1 = make_list(n1);
scanf("%d", &n2);
P2 = make_list(n2);
P_M = mul_link(P1, P2);
P_S = add_link(P1, P2);
Print_link(P_M);
Print_link(P_S);
free_link(P1);
free_link(P2);
free_link(P_M);
free_link(P_S);
return 0;
}
List make_list(int num)
{
List M_p, PtrL, M_t;
int m, p;
M_p = (List)malloc(sizeof(struct LNode));
M_p ->next = NULL;
PtrL = M_p;
if(!num){
attach(0, 0, &PtrL);
}
while(num--){
scanf("%d%d", &m, &p);
attach(m, p, &PtrL);
}
M_t = M_p;
M_p = M_p ->next;
free(M_t);
return M_p;
}
void attach(int x, int p, List *ptrL)
{
List a_p;
a_p = (List)malloc(sizeof(struct LNode));
a_p ->X = x;
a_p ->P = p;
a_p ->next = NULL;
(*ptrL) ->next = a_p;
*ptrL = a_p;
}
List add_link(List P1, List P2)
{
List a_p, T, PtrL, t1, t2;
a_p = (List)malloc(sizeof(struct LNode));
a_p ->next = NULL;
PtrL = a_p;
t1 = P1; t2 = P2;
while(t1 && t2)
{
switch(compare(t1, t2)){
case 1:
attach(t1->X, t1->P, &PtrL);
t1 = t1 ->next;
break;
case -1:
attach(t2->X, t2->P, &PtrL);
t2 = t2 ->next;
break;
case 0:
if((t1->X + t2 ->X) != 0)
attach((t1->X + t2 ->X), t1 ->P, &PtrL);
t1 = t1 ->next;
t2 = t2 ->next;
break;
default:
break;
}
}
for(; t1; t1 = t1 ->next)
attach(t1->X, t1->P, &PtrL);
for(; t2; t2 = t2 ->next)
attach(t2->X, t2->P, &PtrL);
if(!a_p ->next)
attach(0, 0, &PtrL);
T = a_p;
a_p = a_p ->next;
free(T);
return a_p;
}
List mul_link(List P1, List P2)
{
List M_p, Temp, PtrL, t1, t2;
M_p = (List)malloc(sizeof(struct LNode));
M_p ->next = NULL;
PtrL = M_p;
for(t1 = P1; t1; t1=t1 ->next)
{
for(t2 = P2; t2; t2 = t2 ->next)
{
if(!t1->X || !t2 ->X)
break;
attach_link((t1->X * t2 ->X ), (t1->P + t2 ->P), &PtrL);
PtrL = M_p ->next;
}
}
if(!M_p ->next)
attach(0, 0, &PtrL);
Temp = M_p;
M_p = M_p ->next;
free(Temp);
return M_p;
}
int compare(List P1, List P2)
{
if(P1->P > P2->P){
return 1;
}else if(P1->P < P2->P){
return -1;
}else{
return 0;
}
}
void attach_link(int x, int p,List *ptrL)
{
if(x == 0)
return ;
int flag = 0;
List Temp;
List new_p;
for( Temp = (*ptrL); Temp ->next; Temp =Temp ->next){
if(Temp ->next ->P == p){
flag = 1;
Temp ->next ->X += x;
if(Temp ->next ->X == 0){
new_p = Temp ->next;
Temp ->next = Temp ->next ->next;
free(new_p);
}
break;
}
}
if(flag == 0){
while(1){
if(!(*ptrL) ->next)
{
attach_for(x, p, ptrL);
break;
}else if(p > (*ptrL) ->next ->P){
attach_for(x, p, ptrL);
break;
}
(*ptrL) = (*ptrL) ->next;
}
}
return ;
}
void attach_for(int x, int p, List *L)
{
List new_p;
new_p = (List)malloc(sizeof(struct LNode));
new_p ->X = x;
new_p ->P = p;
new_p ->next = (*L) ->next;
(*L) ->next = new_p;
*L = new_p;
}
void free_link(List PtrL)
{
List temp;
if(!PtrL){
return ;
}
while(PtrL){
temp = PtrL;
PtrL = PtrL ->next;
free(temp);
}
return ;
}
void Print_link(List PtrL)
{
List Temp = PtrL;
if(!PtrL)
return ;
while(Temp ->next){
printf("%d %d ", Temp ->X, Temp ->P);
Temp = Temp ->next;
}
printf("%d %d\n", Temp ->X, Temp ->P);
return ;
}