这里用一巨笨的方法做这题,因为这题我做的时间跨度比较大。。。。所以就写得又长又臭,最后勉强ac就没动力再改了。
设计函数分别求两个一元多项式的乘积与和。
输入格式:
输入分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 *PtrToNode;
struct node{
int exponent; /**< 指数 */
int coefficient; /**< 系数 */
PtrToNode next;
};
typedef PtrToNode list;
list read(); /**< 读入链表 */
void showList(list a); /**< 打印链表 */
list multiplication(list a, list b); /**< 计算链表的乘法 */
list findExponent(list a, int expt); /**< 放出含有指数expt的所在的位置 */
void sortList(list a); /**< 按指数从大到小排列链表 */
void swapList(list a, list b); /**< 交换节点中的数据 */
list sum(list a, list b); /**< 计算链表的和 */
void deleteZero(list a); /**< 删除链表中指数为零的项 */
void deleteZero(list a)
{
list tmp = a;
while(tmp->next)
{
if(tmp->next->coefficient == 0)
{
if(tmp->next->next)
{
list last = tmp->next->next;
free(tmp->next);
tmp->next = last;
}
else
{
free(tmp->next);
tmp->next = NULL;
break;
}
}
tmp = tmp->next;
}
}
list sum(list a, list b)
{
list rlt;
rlt = (list)malloc(sizeof(struct node));
rlt->next = NULL;
list cur = rlt;
while(a->next && b->next)
{
list tmp = (list)malloc((sizeof(struct node)));
tmp->next = NULL;
if(a->next->exponent == b->next->exponent)
{
tmp->exponent = a->next->exponent;
tmp->coefficient = a->next->coefficient + b->next->coefficient;
if(tmp->coefficient==0)
{
free(tmp);
}
else
{
cur->next = tmp;
cur = cur->next;
}
a = a->next;
b = b->next;
}
else if(a->next->exponent > b->next->exponent)
{
tmp->exponent = a->next->exponent;
tmp->coefficient = a->next->coefficient;
cur->next = tmp;
cur = cur->next;
a = a->next;
}
else
{
tmp->exponent = b->next->exponent;
tmp->coefficient = b->next->coefficient;
cur->next = tmp;
cur = cur->next;
b = b->next;
}
}
while(a->next)
{
list tmp = (list) malloc(sizeof(struct node));
tmp->next = NULL;
tmp->exponent = a->next->exponent;
tmp->coefficient = a->next->coefficient;
cur->next = tmp;
cur = cur->next;
a = a->next;
}
while(b->next)
{
list tmp = (list) malloc(sizeof(struct node));
tmp->next = NULL;
tmp->exponent = b->next->exponent;
tmp->coefficient = b->next->coefficient;
cur->next = tmp;
cur = cur->next;
b = b->next;
}
return rlt;
}
void swapList(list a, list b)
{
int tmpe;
int tmpc;
tmpe = a->exponent;
a->exponent = b->exponent;
b->exponent = tmpe;
tmpc = a->coefficient;
a->coefficient = b->coefficient;
b->coefficient = tmpc;
}
void sortList(list a)
{
list pos = a;
list cur = pos->next;
int tmp;
list max;
pos = pos->next;
while(pos)
{
max = pos;
cur = pos;
while(cur)
{
if(cur->exponent > max->exponent)
{
swapList(cur, max);
}
cur = cur->next;
//showList(a);
}
pos = pos->next;
}
}
list findExponent(list a, int expt)
{
list tmp = NULL;
while(a->next && a->next->exponent!=expt)
a = a->next;
if(a->next!=NULL)
{
tmp = a;
}
return tmp;
}
list multiplication(list a, list b)
{
list rlt = (list)malloc(sizeof(struct node));
rlt->next = NULL;
if(a->next == NULL)
{
rlt->next = NULL;
}
else if(b->next == NULL)
{
rlt->next = NULL;
}
else
{
list cur = rlt;
list tmpB = b;
while(a->next)
{
tmpB = b;
while(tmpB->next)
{
int expt = (tmpB->next->exponent) + (a->next->exponent);
list tmp = findExponent(rlt, expt);
/**< 如果该指数不存在 */
if(tmp == NULL)
{
tmp = (list)malloc(sizeof(struct node));
tmp->next = NULL;
tmp->coefficient = (tmpB->next->coefficient) * (a->next->coefficient);
tmp->exponent = (tmpB->next->exponent) + (a->next->exponent);
cur->next = tmp;
cur = cur->next;
}
else
{
tmp->next->coefficient = (tmpB->next->coefficient)*(a->next->coefficient) + (tmp->next->coefficient);
/**< 如果该指数存在,并且合并后为0 */
// if(tmp->next->coefficient == 0)
// {
// list last = tmp->next->next;
// free(tmp->next);
// tmp->next = last;
//
// }
}
tmpB = tmpB->next;
}
a = a->next;
}
}
return rlt;
}
list read()
{
int num, i;
scanf("%d", &num);
list head, cur, tmp;
head = (list)malloc(sizeof(struct node));
head->next = NULL;
cur = head;
for(i=0; i<num; i++)
{
tmp = (list)malloc(sizeof(struct node));
scanf("%d %d", &tmp->coefficient, &tmp->exponent);
tmp->next = NULL;
cur->next = tmp;
cur = cur->next;
}
return head;
}
void showList(list a)
{
if(a->next == NULL)
printf("0 0");
while(a->next)
{
a = a->next;
printf("%d ", a->coefficient);
printf("%d", a->exponent);
if(a->next)
printf(" ");
}
}
int main()
{
list one;
list two;
list three;
list four;
one = read();
two = read();
three = multiplication(one , two);
//showList(three);
if(three->next)
{
sortList(three);
deleteZero(three);
}
showList(three);
printf("\n");
four = sum(one, two);
showList(four);
system("pause");
}