PTA 6-14 用单向链表完成多项式运算 (35 分)
Posted graytido
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PTA 6-14 用单向链表完成多项式运算 (35 分)相关的知识,希望对你有一定的参考价值。
输入两个多项式,计算它们的加、减及乘法, 将计算结果输出到屏幕上。
1) 输入:从键盘分两行分别输入两个多项式数据,每个多项式输入格式如下:
n a1 m1 a2 m2 a3 m3 . .. ai mi.... an mn
其中n为多项式系数为非零的项数,ai为系数,mi为指数, 它们都是整数,该输入数据所表达的多项式意义为(其中符号^表示幂次):
a1x^m1 + a2x^m2 + a3x^m3 + ...... + an x^mn
2)输出:先输出参与运算的两个多项式, 然后输出它们的运算结果,格式为:
要求:多项式输出时从幂次数高到低的顺序逐项输出,若任何一项的系数是0的话,请不要输出,除非所有的系数为0,则输出0。
函数接口定义:
void print_polynomial(polynomial *head);
本函数完成一个多项式的输出。题目要求的功能以及本函数具体涉及到多项式链表的创建以及多项式加、减和乘法运算所需的功能模块请在本函数实现之前一并自行完成,并将代码插入在合适的位置。
裁判测试程序样例:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node
int coef; /* 系数 */
int exp; /* 指数 */
struct node *next;
polynomial;
/* 提示:递交的答案将会自动插入此处 */
int main(void)
polynomial *polyA,*polyB;
polyA=create_polynomial(); /* 其中函数create_polynomial()用于读入并创建多项式A的链表,请自行实现 */
polyB=create_polynomial(); /* 读入并创建多项式B的链表 */
printf("A=");
print_polynomial(polyA); /* 输出多项式A */
printf("B=");
print_polynomial(polyB); /* 输出多项式B */
printf("A+B=");
print_polynomial(add_polynomial(polyA,polyB) );
/* 输出多项式加A+B 的结果, 其中函数add_polynomial(polyA,polyB)将返回A+B的多项式,请自行实现*/
printf("A-B=");
print_polynomial(subtract_polynomial(polyA,polyB) );
/* 输出多项式减A-B 的结果,其中函数subtract_polynomial(polyA,polyB)返回A-B的多项式,请自行实现*/
printf("A*B=");
print_polynomial(multiply_polynomial(polyA,polyB));
/* 输出多项式乘A*B 的结果,其中函数multiply_polynomial(polyA,polyB)返回A*B的多项式,请自行实现 */
return 0;
/* 请在这里填写答案 */
输入样例:
在这里给出一组输入。例如:
3 1 2 3 4 5 6
3 1 2 3 4 5 6
输出样例:
在这里给出相应的输出。例如(其中 x 是小写字母):
A=5x^6+3x^4+x^2
B=5x^6+3x^4+x^2
A+B=10x^6+6x^4+2x^2
A-B=0
AB=25x^12+30x^10+19x^8+6x^6+x^4
有点憨的题目,放在这做个备忘...到现在还没过掉全部样例,应该是还有几种情况没考虑到 (也可能根本就写错了,要是有人写出来了 ...请一定抬我一手
下面的代码只能得20分(写得极丑,本来还想写对了再改.....*
/* void print_polynomial(polynomial *head)
polynomial *p;
int flag = 0;
for (p = head; p != NULL; p = p->next)
if (!flag && p->coef != 0)
flag = 1;
if (p->coef > 0)
p->coef == 1 ? printf("x") : printf("%dx", p->coef);
printf("^%d", p->exp);
else
p->coef == -1 ? printf("-x") : printf("%dx", p->coef);
printf("^%d", p->exp);
else if (flag && p->coef != 0)
if (p->coef > 0)
p->coef == 1 ? printf("+x") : printf("+%dx", p->coef);
printf("^%d", p->exp);
else
p->coef == -1 ? printf("-x") : printf("%dx", p->coef);
printf("^%d", p->exp);
if (!flag)
printf("0");
printf("\n");
*/
void print_polynomial(polynomial *head)
polynomial *p;
int flag = 0;
for (p = head; p != NULL; p = p->next)
if (!flag && p->coef != 0)
flag = 1;
if (p->coef > 0)
if (p->exp == 0)
printf("%d", p->coef);
else
p->coef == 1 ? printf("x") : printf("%dx", p->coef);
p->exp == 1 ? printf("") : printf("^%d", p->exp);
else
if (p->exp == 0)
printf("%d", p->coef);
else
p->coef == -1 ? printf("-x") : printf("%dx", p->coef);
p->exp == 1 ? printf("") : printf("^%d", p->exp);
else if (flag && p->coef != 0)
if (p->coef > 0)
if (p->exp == 0)
printf("+%d", p->coef);
else
p->coef == 1 ? printf("+x") : printf("+%dx", p->coef);
p->exp == 1 ? printf("") : printf("^%d", p->exp);
else
if (p->exp == 0)
printf("%d", p->coef);
else
p->coef == -1 ? printf("-x") : printf("%dx", p->coef);
p->exp == 1 ? printf("") : printf("^%d", p->exp);
if (!flag)
printf("0");
printf("\n");
/* polynomial *create_polynomial()
polynomial a[10000];
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%d%d", &a[i].coef, &a[i].exp);
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - i - 1; j++)
if (a[j].exp > a[j + 1].exp)
polynomial temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
polynomial *head = NULL, *p = NULL;
for (int i = 0; i < n; i++)
p = (polynomial *)malloc(sizeof(polynomial));
p->coef = a[i].coef;
p->exp = a[i].exp;
p->next = head;
head = p;
return head;
*/
polynomial *create_polynomial()
int cah[20000] = 0;
int n;
scanf("%d", &n);
int maxx = -1;
int minn = 300001;
for (int i = 0; i < n; i++)
int co, ex;
scanf("%d%d", &co, &ex);
cah[ex + 10000] += co;
maxx = maxx < ex + 10000 ? ex + 10000 : maxx;
minn = minn > ex + 10000 ? ex + 10000 : minn;
polynomial *head = NULL, *p = NULL;
for (int i = minn; i <= maxx; i++)
p = (polynomial *)malloc(sizeof(polynomial));
p->coef = cah[i];
p->exp = i - 10000;
p->next = head;
head = p;
return head;
/* polynomial *create_polynomial()
polynomial *head = NULL, *p, *tail = NULL;
int n;
scanf("%d", &n);
while (n--)
p = (polynomial *)malloc(sizeof(polynomial));
scanf("%d%d", &p->coef, &p->exp);
p->next = head;
head = p;
return head;
*/
polynomial *add_polynomial(polynomial *a, polynomial *b)
polynomial *head = NULL, *p, *tail = NULL;
polynomial *t1 = a, *t2 = b;
while (t1 && t2)
p = (polynomial *)malloc(sizeof(polynomial));
if (t1->exp > t2->exp)
p->coef = t1->coef;
p->exp = t1->exp;
t1 = t1->next;
else if (t1->exp == t2->exp)
p->coef = t1->coef + t2->coef;
p->exp = t1->exp;
t1 = t1->next;
t2 = t2->next;
else
p->coef = t2->coef;
p->exp = t2->exp;
t2 = t2->next;
if (!head)
head = p;
head->next = NULL;
tail = head;
else
tail->next = p;
tail = tail->next;
tail->next = NULL;
while (t1)
p = (polynomial *)malloc(sizeof(polynomial));
p->coef = t1->coef;
p->exp = t1->exp;
t1 = t1->next;
if (!head)
head = p;
head->next = NULL;
tail = head;
else
tail->next = p;
tail = tail->next;
tail->next = NULL;
while (t2)
p = (polynomial *)malloc(sizeof(polynomial));
p->coef = t2->coef;
p->exp = t2->exp;
t2 = t2->next;
if (!head)
head = p;
head->next = NULL;
tail = head;
else
tail->next = p;
tail = tail->next;
tail->next = NULL;
return head;
polynomial *subtract_polynomial(polynomial *a, polynomial *b)
polynomial *head = NULL, *p, *tail = NULL;
polynomial *t1 = a, *t2 = b;
while (t1 && t2)
p = (polynomial *)malloc(sizeof(polynomial));
if (t1->exp > t2->exp)
p->coef = t1->coef;
p->exp = t1->exp;
t1 = t1->next;
else if (t1->exp == t2->exp)
p->coef = t1->coef - t2->coef;
p->exp = t1->exp;
t1 = t1->next;
t2 = t2->next;
else
p->coef = -t2->coef;
p->exp = t2->exp;
t2 = t2->next;
if (!head)
head = p;
head->next = NULL;
tail = head;
else
tail->next = p;
tail = tail->next;
tail->next = NULL;
while (t1)
p = (polynomial *)malloc(sizeof(polynomial));
p->coef = t1->coef;
p->exp = t1->exp;
t1 = t1->next;
if (!head)
head = p;
head->next = NULL;
tail = head;
else
tail->next = p;
tail = tail->next;
tail->next = NULL;
while (t2)
p = (polynomial *)malloc(sizeof(polynomial));
p->coef = t2->coef;
p->exp = t2->exp;
t2 = t2->next;
if (!head)
head = p;
head->next = NULL;
tail = head;
else
tail->next = p;
tail = tail->next;
tail->next = NULL;
return head;
polynomial *multiply_polynomial(polynomial *a, polynomial *b)
int cach[20000] = 0; //辅助数组
polynomial *head = NULL, *p;
polynomial *t1, *t2;
int maxx = 0;
int minn = 30001;
for (t1 = a; t1 != NULL; t1 = t1->next)
for (t2 = b; t2 != NULL; t2 = t2->next)
cach[t1->exp + t2->exp + 10000] += t1->coef * t2->coef;
maxx = maxx < t1->exp + t2->exp + 10000 ? t1->exp + t2->exp + 10000 : maxx;
minn = minn > t1->exp + t2->exp + 10000 ? t1->exp + t2->exp + 10000 : minn;
for (int i = minn; i <= maxx; i++)
if (cach[i])
p = (polynomial *)malloc(sizeof(polynomial));
p->exp = i - 10000;
p->coef = cach[i];
p->next = head;
head = p;
return head;
以上是关于PTA 6-14 用单向链表完成多项式运算 (35 分)的主要内容,如果未能解决你的问题,请参考以下文章