1071: 数据结构作业01 -- 一元多项式的求积
时间限制: 1 Sec 内存限制: 128 MB提交: 48 解决: 8
[ 提交][ 状态][ 论坛]
题目描述
一个一元多项式可以看作由若干个一元单项式按降幂排列成的线性表。请编写程序对输入的两个一元多项式求积,并输出求积的结果。
输入
输入为两个一元多项式,每个一元多项式输入一行,按照降幂依次输入每个单项式的系数和指数,并以-1 -1作为结束。 系数和指数均为整数,指数不小于0。
输出
输出为求积结果多项式,按照降幂依次输出每个单项的系数和指数,每个数值后面用一个空格隔开,输出结果多项式后换行。 系数为0的单项式不得输出——除非结果多项式就是0,则直接输出0。
样例输入
2 5 1 0 -1 -15 4 3 0 -1 -1
样例输出
10 9 6 5 5 4 3 0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef int Elemtype;
typedef struct Node{
Elemtype cof;
Elemtype inx;
struct Node *next;
}Node, *Linklist;
Status visit(Elemtype a, Elemtype b)
{
printf("%d %d", a, b);
return OK;
}
Status InitList(Linklist *L)
{
(*L) = (Linklist)malloc (sizeof(Node));
if(!(*L))
return ERROR;
(*L)->next = NULL;
return OK;
}
int ListLength(Linklist L)
{
int i;
Linklist p;
p = L->next;
i = 0;
while(p){
i++;
p = p->next;
}
return i;
}
Status GetElem(Linklist L, int i, Elemtype *cof, Elemtype *inx)
{
Linklist p;
int j;
p = L;
j = 1;
while(p && j < i){
p = p->next;
j++;
}
if(!p || j > i){
return ERROR;
}
*cof = p->next->cof;
*inx = p->next->inx;
return OK;
}
int ElemLocate(Linklist L, Elemtype cof, Elemtype inx)
{
Linklist p;
int i;
i = 1;
p = L->next;
while(p && (p->cof != cof || p->inx != inx)){
p = p->next;
i++;
}
if(i <= ListLength(L))
return i;
else
return 0;
}
Status ListInsert(Linklist *L, int i, Elemtype cof, Elemtype inx)
{
if(i < 1 || i > ListLength(*L) + 1)
return ERROR;
int j;
Linklist p, q;
q = (Linklist)malloc (sizeof(Node));
p = *L;
j = 1;
while(p && j < i){
p = p->next;
++j;
}
if(!p || j > i)
return ERROR;
q->next = p->next;
p->next = q;
q->cof = cof;
q->inx = inx;
return OK;
}
Status ListDelete(Linklist *L, int i, Elemtype *cof, Elemtype *inx)
{
int j;
Linklist p, q;
p = *L;
j = 1;
while(p && j < i){
p = p->next;
++j;
}
if(!p || j > i)
return ERROR;
q = p->next;
p->next = p->next->next;
free(q);
return OK;
}
Status ClearList(Linklist *L)
{
Linklist p, q;
p = (*L)->next;
while(p){
q = p;
p = p->next;
free(q);
}
(*L)->next = NULL;
return OK;
}
Status ListEmpty(Linklist L)
{
if(L->next == NULL)
return TRUE;
return FALSE;
}
Status ListTraverse(Linklist L)
{
Linklist p;
int juge;
juge = 0;
p = L->next;
while(p){
juge = p->cof;
if(juge)
break;
p = p->next;
}
if(!juge){
printf("0\n");
return OK;
}
p = L->next;
while(p && p->next != NULL){
if(p->cof != 0){
visit(p->cof, p->inx);
printf(" ");
}
p = p->next;
}
visit(p->cof, p->inx);
printf("\n");
return OK;
}
Status PopList(Linklist *L)
{
Linklist p, q;
Elemtype temp;
for(p = (*L)->next; p != NULL; p = p->next){
for(q = p->next; q != NULL; q = q->next){
if(p->inx < q->inx){
temp = p->inx;
p->inx = q->inx;
q->inx = temp;
temp = p->cof;
p->cof = q->cof;
q->cof = temp;
}
}
}
return OK;
}
Status FinalList(Linklist *L)
{
Linklist p, q;
//q = (Linklist *)malloc (sizeof(Node));
p = (*L)->next;
while(p){
if(p->next != NULL && p->next->inx == p->inx){
p->cof += p->next->cof;
q = p->next;
p->next = p->next->next;
free(q);
continue;
}
p = p->next;
}
return OK;
}
Status MulList(Linklist *L3, Linklist L1, Linklist L2)
{
int i;//L1循环
int j;//L2循环
int k;//L3循环
Elemtype cof1, inx1;
Elemtype cof2, inx2;
Elemtype cof3, inx3;
//int *num;
k = 1;
for(i = 1; i <= ListLength(L1); i++){
for(j = 1; j <= ListLength(L2); j++){
GetElem(L1, i, &cof1, &inx1);
GetElem(L2, j, &cof2, &inx2);
cof3 = cof1 * cof2;
inx3 = inx1 + inx2;
ListInsert(L3, k++, cof3, inx3);
}
}
PopList(L3);
return OK;
}
int main()
{
Linklist L1, L2, L3;
int cofn, inxn;//输入的系数和指数
int i;//循环
InitList(&L1);
InitList(&L2);
InitList(&L3);
i = 1;
while(scanf("%d%d", &cofn, &inxn), inxn != -1){
ListInsert(&L1, i++, cofn, inxn);
}
i = 1;
while(scanf("%d%d", &cofn, &inxn), cofn != -1){
ListInsert(&L2, i++, cofn, inxn);
}
MulList(&L3, L1, L2);
FinalList(&L3);
ListTraverse(L3);
return 0;
}
/*1071
=================/test0.out
Right:
-91 2000 4484 1999 7401 1998 2023 1997 -1022
-----------------
Your:
0
=================
=================/test1.out
Right:
-10 10 -40 8 -40 6 -20 5 -40 3 -10 0
-----------------
Your:
0
=================*/