版权声明: https://blog.csdn.net/acDream_/article/details/82730484
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
思路:
1.使用单链表,设置结点Node,成员变量为系数和指数以及下一个指向的对象
2.先来说一下多项式加法(比较简单)
1)若指数相同则系数相加,添加到链表c中(不要忘记特判系数相加为0的情况这时候不相加)
2)因为要有序,所以比较指数时,若该a链表当前结点的指数比b链表当前结点的指数大时,则把a链表当前结点添加到链表c中,然后让a链表当前结点往后移,链表c当前位置也往后移,同理链表b当前结点比链表a当前结点指数大的时候是一样的道理
3)把剩余的结点加入到链表c中(pc.next=(pa!=null?pa:pb))
3.多项式乘法(可能我写的比较麻烦了)
1)先将多项式a的第一项与多项式b的每一项相乘,把每一项都添加到新建的链表c中
2)将多项式a余下的项与多项式b进行相乘,把相乘的项与链表c中每一项进行比较,若该项的指数与链表c中某一项的指数相同,则进行系数相加(这里需要特判,如果系数相加为0则去掉链表c中的该项),若该项系数要大于链表c中的某一项,则添加进去,若该项系数比链表c每一项的系数都要小的话,则添加到链表c的末位
其中需要特别注意的是:
当特判如果系数相加为0则去掉链表c中的该项时,若该项是链表c中的尾结点,去掉该尾结点之后会让当前结点的指向下一结点为空,程序认为是该项系数比链表c每一项系数都要小,会添加到链表c的末位,这个BUG我找了好久,哭叽叽,所以只需要添加一个布尔类型的变量来进行判断就行,初始值设为true,若是合并后下一结点为空,则把该变量设为false就行
if(curPc.next==null && is) {
curPc.next = new Node(xs, zs);
}
4. 实现了多项式的乘法与加法计算后别以为就完事了,输出的时候还需要判别一下零多项式,若系数全是0的话就输出0 0就行
下面是AcCode:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int lengthA = in.nextInt();
int[][] numA = new int[lengthA][2];
for (int i = 0; i < numA.length; i++) {
for (int j = 0; j < numA[i].length; j++) {
numA[i][j] = in.nextInt();
}
}
LinkList linkListA = new LinkList(numA);
int lengthB = in.nextInt();
int[][] numB = new int[lengthB][2];
for (int i = 0; i < numB.length; i++) {
for (int j = 0; j < numB[i].length; j++) {
numB[i][j] = in.nextInt();
}
}
LinkList linkListB = new LinkList(numB);
Node mutiply = linkListA.hbMutiplly(linkListA, linkListB);
Node sum = linkListA.hbSum(linkListA, linkListB);
mutiply = mutiply.next;
boolean is = true;//是不是第一个元素
boolean isZero = true;
while(mutiply!=null) {
if(is) {
if(mutiply.xs!=0) {
System.out.print(mutiply.xs+" "+mutiply.zs);
is = false;
isZero = false;
}
}else {
if(mutiply.xs!=0) {
System.out.print(" "+mutiply.xs+" "+mutiply.zs);
isZero = false;
}
}
mutiply = mutiply.next;
}
if(isZero) {
System.out.print(0+" "+0);
}
System.out.println();
is = true;
isZero = true;
sum = sum.next;
while(sum!=null) {
if(is) {
if(sum.xs!=0) {
System.out.print(sum.xs+" "+sum.zs);
is = false;
isZero = false;
}
}else {
if(sum.xs!=0) {
System.out.print(" "+sum.xs+" "+sum.zs);
isZero = false;
}
}
sum = sum.next;
}
if(isZero) {
System.out.print("0 0");
}
System.out.println();
}
}
class LinkList {
Node head;// 头结点
/**
* 多项式乘积
* @param linkListA
* @param linkListB
* @return
*/
public Node hbMutiplly(LinkList linkListA,LinkList linkListB) {
Node pa = linkListA.head.next;
Node pb = linkListB.head.next;
Node pc = new Node();
Node curPc = pc;
//初始化
if(pa!=null) {
while(pb!=null) {
curPc.next = new Node(pa.xs*pb.xs, pa.zs+pb.zs);
curPc = curPc.next;
pb = pb.next;
}
pa = pa.next;
}
while(pa!=null) {
pb = linkListB.head.next;
curPc = pc;
while(pb!=null) {
int xs = pa.xs*pb.xs;
int zs = pa.zs+pb.zs;
boolean is = true;
while(curPc.next!=null) {
if(curPc.next.zs==zs) {
if((curPc.next.xs+xs)!=0) {
curPc.next.xs = curPc.next.xs+xs;
}else {
curPc.next = curPc.next.next;
is = false;
}
break;
}else if(curPc.next.zs<zs) {
curPc.next = new Node(xs, zs, curPc.next);
break;
}
curPc = curPc.next;
}
if(curPc.next==null && is) {
curPc.next = new Node(xs, zs);
}
// Node c = pc.next;
// while (c!= null) {
// System.out.print(c.xs + " " + c.zs + " ");
// c = c.next;
// }
// System.out.println();
pb = pb.next;
}
pa = pa.next;
}
return pc;
}
/**
* 多项式加法
* @param linkListA
* @param linkListB
* @return
*/
public Node hbSum(LinkList linkListA, LinkList linkListB) {
Node pa = linkListA.head.next;
Node pb = linkListB.head.next;
Node NodeCHead = new Node();// 存放多项式的和
Node pc = NodeCHead;
while (pa != null && pb != null) {
if (pa.zs == pb.zs) {
if ((pa.xs + pb.xs) != 0) {
pc.next = new Node(pa.xs + pb.xs, pa.zs);
pc = pc.next;
}
pa = pa.next;
pb = pb.next;
} else if (pa.zs > pb.zs) {
pc.next = pa;
pc = pc.next;
pa = pa.next;
} else {
pc.next = pb;
pc = pc.next;
pb = pb.next;
}
}
pc.next = (pa != null) ? pa : pb;
return NodeCHead;
}
public void display() {
Node p = head.next;
while (p != null) {
System.out.print(p.xs + " " + p.zs + " ");
p = p.next;
}
System.out.println();
}
public LinkList() {
this.head = new Node();
}
public LinkList(int[][] num) {
this();
Node p = head;
for (int i = 0; i < num.length; i++) {
p.next = new Node(num[i][0],num[i][1], p.next);
p = p.next;
}
}
}
class Node {
int xs;
int zs;
Node next;
/**
* 初始化头结点
*/
public Node() {
this.next = null;
}
public Node(int xs, int zs, Node next) {
this(xs, zs);
this.next = next;
}
/**
* 尾结点
*
* @param xs
* @param zs
*/
public Node(int xs, int zs) {
this();
this.xs = xs;
this.zs = zs;
}
}
最后偷偷附上测试数据:
序号 | 输入 | 输出 |
---|---|---|
1 | 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 |
2 | 2 1 2 1 0 2 1 2 -1 0 |
1 4 -1 0 2 2 |
3 | 2 -1000 1000 1000 0 2 1000 1000 -1000 0 |
-1000000 2000 2000000 1000 -1000000 0 0 0 |
4 | 0 1 999 1000 |
0 0 999 1000 |