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
A*B=25x^12+30x^10+19x^8+6x^6+x^4
有点憨的题目,放在这做个备忘...到现在还没过掉全部样例,应该是还有几种情况没考虑到(但是就是想不到),要是有人写出来了...请一定抬我一手
下面的代码只能得20分
技术图片
  1 /* void print_polynomial(polynomial *head)
  2 
  3     polynomial *p;
  4     int flag = 0;
  5     for (p = head; p != NULL; p = p->next)
  6     
  7         if (!flag && p->coef != 0)
  8         
  9             flag = 1;
 10             if (p->coef > 0)
 11             
 12                 p->coef == 1 ? printf("x") : printf("%dx", p->coef);
 13                 printf("^%d", p->exp);
 14             
 15             else
 16             
 17                 p->coef == -1 ? printf("-x") : printf("%dx", p->coef);
 18                 printf("^%d", p->exp);
 19             
 20         
 21         else if (flag && p->coef != 0)
 22         
 23             if (p->coef > 0)
 24             
 25                 p->coef == 1 ? printf("+x") : printf("+%dx", p->coef);
 26                 printf("^%d", p->exp);
 27             
 28             else
 29             
 30                 p->coef == -1 ? printf("-x") : printf("%dx", p->coef);
 31                 printf("^%d", p->exp);
 32             
 33         
 34     
 35     if (!flag)
 36         printf("0");
 37     printf("\n");
 38  */
 39 void print_polynomial(polynomial *head)
 40 
 41     polynomial *p;
 42     int flag = 0;
 43     for (p = head; p != NULL; p = p->next)
 44     
 45         if (!flag && p->coef != 0)
 46         
 47             flag = 1;
 48             if (p->coef > 0)
 49             
 50                 if (p->exp == 0)
 51                     printf("%d", p->coef);
 52                 else
 53                 
 54                     p->coef == 1 ? printf("x") : printf("%dx", p->coef);
 55                     p->exp == 1 ? printf("") : printf("^%d", p->exp);
 56                 
 57             
 58             else
 59             
 60                 if (p->exp == 0)
 61                     printf("%d", p->coef);
 62                 else
 63                 
 64                     p->coef == -1 ? printf("-x") : printf("%dx", p->coef);
 65                     p->exp == 1 ? printf("") : printf("^%d", p->exp);
 66                 
 67             
 68         
 69         else if (flag && p->coef != 0)
 70         
 71             if (p->coef > 0)
 72             
 73                 if (p->exp == 0)
 74                     printf("+%d", p->coef);
 75                 else
 76                 
 77                     p->coef == 1 ? printf("+x") : printf("+%dx", p->coef);
 78                     p->exp == 1 ? printf("") : printf("^%d", p->exp);
 79                 
 80             
 81             else
 82             
 83                 if (p->exp == 0)
 84                     printf("%d", p->coef);
 85                 else
 86                 
 87                     p->coef == -1 ? printf("-x") : printf("%dx", p->coef);
 88                     p->exp == 1 ? printf("") : printf("^%d", p->exp);
 89                 
 90             
 91         
 92     
 93     if (!flag)
 94         printf("0");
 95     printf("\n");
 96 
 97 /* polynomial *create_polynomial()
 98 
 99     polynomial a[10000];
100     int n;
101     scanf("%d", &n);
102     for (int i = 0; i < n; i++)
103         scanf("%d%d", &a[i].coef, &a[i].exp);
104     for (int i = 0; i < n - 1; i++)
105         for (int j = 0; j < n - i - 1; j++)
106             if (a[j].exp > a[j + 1].exp)
107             
108                 polynomial temp = a[j];
109                 a[j] = a[j + 1];
110                 a[j + 1] = temp;
111             
112     polynomial *head = NULL, *p = NULL;
113     for (int i = 0; i < n; i++)
114     
115         p = (polynomial *)malloc(sizeof(polynomial));
116         p->coef = a[i].coef;
117         p->exp = a[i].exp;
118         p->next = head;
119         head = p;
120     
121     return head;
122  */
123 polynomial *create_polynomial()
124 
125     int cah[20000] = 0;
126     int n;
127     scanf("%d", &n);
128     int maxx = -1;
129     int minn = 300001;
130     for (int i = 0; i < n; i++)
131     
132         int co, ex;
133         scanf("%d%d", &co, &ex);
134         cah[ex + 10000] += co;
135         maxx = maxx < ex + 10000 ? ex + 10000 : maxx;
136         minn = minn > ex + 10000 ? ex + 10000 : minn;
137     
138     polynomial *head = NULL, *p = NULL;
139     for (int i = minn; i <= maxx; i++)
140     
141         p = (polynomial *)malloc(sizeof(polynomial));
142         p->coef = cah[i];
143         p->exp = i - 10000;
144         p->next = head;
145         head = p;
146     
147     return head;
148 
149 /* polynomial *create_polynomial()
150 
151     polynomial *head = NULL, *p, *tail = NULL;
152     int n;
153     scanf("%d", &n);
154     while (n--)
155     
156         p = (polynomial *)malloc(sizeof(polynomial));
157         scanf("%d%d", &p->coef, &p->exp);
158         p->next = head;
159         head = p;
160     
161     return head;
162  */
163 polynomial *add_polynomial(polynomial *a, polynomial *b)
164 
165     polynomial *head = NULL, *p, *tail = NULL;
166     polynomial *t1 = a, *t2 = b;
167     while (t1 && t2)
168     
169         p = (polynomial *)malloc(sizeof(polynomial));
170         if (t1->exp > t2->exp)
171         
172             p->coef = t1->coef;
173             p->exp = t1->exp;
174             t1 = t1->next;
175         
176         else if (t1->exp == t2->exp)
177         
178             p->coef = t1->coef + t2->coef;
179             p->exp = t1->exp;
180             t1 = t1->next;
181             t2 = t2->next;
182         
183         else
184         
185             p->coef = t2->coef;
186             p->exp = t2->exp;
187             t2 = t2->next;
188         
189         if (!head)
190         
191             head = p;
192             head->next = NULL;
193             tail = head;
194         
195         else
196         
197             tail->next = p;
198             tail = tail->next;
199             tail->next = NULL;
200         
201     
202     while (t1)
203     
204         p = (polynomial *)malloc(sizeof(polynomial));
205         p->coef = t1->coef;
206         p->exp = t1->exp;
207         t1 = t1->next;
208         if (!head)
209         
210             head = p;
211             head->next = NULL;
212             tail = head;
213         
214         else
215         
216             tail->next = p;
217             tail = tail->next;
218             tail->next = NULL;
219         
220     
221     while (t2)
222     
223         p = (polynomial *)malloc(sizeof(polynomial));
224         p->coef = t2->coef;
225         p->exp = t2->exp;
226         t2 = t2->next;
227         if (!head)
228         
229             head = p;
230             head->next = NULL;
231             tail = head;
232         
233         else
234         
235             tail->next = p;
236             tail = tail->next;
237             tail->next = NULL;
238         
239     
240     return head;
241 
242 polynomial *subtract_polynomial(polynomial *a, polynomial *b)
243 
244     polynomial *head = NULL, *p, *tail = NULL;
245     polynomial *t1 = a, *t2 = b;
246     while (t1 && t2)
247     
248         p = (polynomial *)malloc(sizeof(polynomial));
249         if (t1->exp > t2->exp)
250         
251             p->coef = t1->coef;
252             p->exp = t1->exp;
253             t1 = t1->next;
254         
255         else if (t1->exp == t2->exp)
256         
257             p->coef = t1->coef - t2->coef;
258             p->exp = t1->exp;
259             t1 = t1->next;
260             t2 = t2->next;
261         
262         else
263         
264             p->coef = -t2->coef;
265             p->exp = t2->exp;
266             t2 = t2->next;
267         
268         if (!head)
269         
270             head = p;
271             head->next = NULL;
272             tail = head;
273         
274         else
275         
276             tail->next = p;
277             tail = tail->next;
278             tail->next = NULL;
279         
280     
281     while (t1)
282     
283         p = (polynomial *)malloc(sizeof(polynomial));
284         p->coef = t1->coef;
285         p->exp = t1->exp;
286         t1 = t1->next;
287         if (!head)
288         
289             head = p;
290             head->next = NULL;
291             tail = head;
292         
293         else
294         
295             tail->next = p;
296             tail = tail->next;
297             tail->next = NULL;
298         
299     
300     while (t2)
301     
302         p = (polynomial *)malloc(sizeof(polynomial));
303         p->coef = t2->coef;
304         p->exp = t2->exp;
305         t2 = t2->next;
306         if (!head)
307         
308             head = p;
309             head->next = NULL;
310             tail = head;
311         
312         else
313         
314             tail->next = p;
315             tail = tail->next;
316             tail->next = NULL;
317         
318     
319     return head;
320 
321 polynomial *multiply_polynomial(polynomial *a, polynomial *b)
322 
323     int cach[20000] = 0; //辅助数组
324     polynomial *head = NULL, *p;
325     polynomial *t1, *t2;
326     int maxx = 0;
327     int minn = 30001;
328     for (t1 = a; t1 != NULL; t1 = t1->next)
329     
330         for (t2 = b; t2 != NULL; t2 = t2->next)
331         
332             cach[t1->exp + t2->exp + 10000] += t1->coef * t2->coef;
333             maxx = maxx < t1->exp + t2->exp + 10000 ? t1->exp + t2->exp + 10000 : maxx;
334             minn = minn > t1->exp + t2->exp + 10000 ? t1->exp + t2->exp + 10000 : minn;
335         
336     
337     for (int i = minn; i <= maxx; i++)
338     
339         if (cach[i])
340         
341             p = (polynomial *)malloc(sizeof(polynomial));
342             p->exp = i - 10000;
343             p->coef = cach[i];
344             p->next = head;
345             head = p;
346         
347     
348     return head;
349 
View Code

以上是关于PTA 6-14 用单向链表完成多项式运算 (35 分)的主要内容,如果未能解决你的问题,请参考以下文章

数据结构之利用单向链表实现多项式加法和乘法

PTA-数据结构与算法基础题目集(中文)

实现多项式的加法和乘法运算

[PTA]一元多项式运算器

2.用链表表示多项式

PTA 02-线性结构2 一元多项式的乘法与加法运算