02-线性结构2 一元多项式的乘法与加法运算 (20 分)
Posted interim
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了02-线性结构2 一元多项式的乘法与加法运算 (20 分)相关的知识,希望对你有一定的参考价值。
02-线性结构2 一元多项式的乘法与加法运算 (20 分)
设计函数分别求两个一元多项式的乘积与和。
输入格式:
输入分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
using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using System.Diagnostics; using System.Net; using System.Text; public class ONE { public ONE(string line) { try { var temp = line.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries); for (int i = 1; i < temp.Length; i++) { list.Add(new Point(int.Parse(temp[i]), int.Parse(temp[++i]))); } } catch (Exception) { throw; } } List<Point> list = new List<Point>(); public static ONE operator *(ONE o1, ONE o2) { ONE newOne = new ONE(""); for (int i = 0; i < o1.list.Count; i++) { for (int j = 0; j < o2.list.Count; j++) { newOne.list.Add(new Point(o1.list[i].X * o2.list[j].X, o1.list[i].Y + o2.list[j].Y)); } } if (newOne.list.Count == 0) newOne.list.Add(new Point(0, 0)); return Make(newOne); } public static ONE operator +(ONE o1, ONE o2) { ONE newOne = new ONE(""); newOne.list.AddRange(o1.list); newOne.list.AddRange(o2.list); if (newOne.list.Count == 0) newOne.list.Add(new Point(0, 0)); return Make(newOne); } static int comparer(Point o1, Point o2) { return o2.Y.CompareTo(o1.Y); } static ONE Make(ONE one) { int c = 0; AAA: c = one.list.Count; one.list.Sort(comparer); for (int i = one.list.Count - 1; i >= 0; i--) { for (int j = i - 1; j >= 0; j--) { if (one.list[i].Y == one.list[j].Y) { one.list[j] = new Point(one.list[j].X + one.list[i].X, one.list[j].Y); one.list.RemoveAt(i); break; } else break; } } if (c != one.list.Count) goto AAA; for (int i = one.list.Count - 1; i >= 0; i--) { if(one.list[i].X==0) { one.list.RemoveAt(i); } } return one; } public void OUT() { StringBuilder sb = new StringBuilder(); if (list.Count == 0) list.Add(new Point(0, 0)); foreach (var item in list) { sb.Append(item.X + " " + item.Y + " "); } Console.WriteLine(sb.ToString().Trim()); } } struct Point { public int X; public int Y; public Point(int v1, int v2) : this() { this.X = v1; this.Y = v2; } } class T { static void Main(string[] args) { ONE a = new ONE(Console.ReadLine()); ONE a2 = new ONE(Console.ReadLine()); (a * a2).OUT(); (a + a2).OUT(); } }
以上是关于02-线性结构2 一元多项式的乘法与加法运算 (20 分)的主要内容,如果未能解决你的问题,请参考以下文章