题目大意
设计函数分别求两个一元多项式的乘积与和。
输入格式:
输入分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
解析:整个代码最难的地方就是插入,按照eps的大小进行插入,并且cof为0要删除这个结点。
AC代码:
import java.util.Scanner;
class Polynomial {
private int cof;
private int eps;
public Polynomial(int cof, int eps) {
this.cof = cof;
this.eps = eps;
}
public int getCof() {
return cof;
}
public void setCof(int cof) {
this.cof = cof;
}
public int getEps() {
return eps;
}
public void setEps(int eps) {
this.eps = eps;
}
public Polynomial PolySummation(Polynomial rhs) {
return (new Polynomial(this.getCof() + rhs.getCof(), this.getEps()));
}
public Polynomial PolyMultiplication(Polynomial rhs) {
return (new Polynomial(this.getCof() * rhs.getCof(), this.getEps() + rhs.getEps()));
}
}
class LNode {
private Polynomial data;
private LNode next;
public LNode() {
this.data = new Polynomial(0, 0);
this.next = null;
}
public LNode(Polynomial data) {
this.data = data;
this.next = null;
}
public Polynomial getData() {
return this.data;
}
public void setData(Polynomial data) {
this.data = data;
}
public LNode getNext() {
return this.next;
}
public void setNext(LNode next) {
this.next = next;
}
}
class LinkList {
private LNode head;
public LinkList() {
this.head = new LNode();
}
public LNode getHead() {
return this.head;
}
public void InitList() {
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
int x = sc.nextInt(), y = sc.nextInt();
this.Insert(new Polynomial(x, y));
}
}
public void DeleteLNode(LNode pos) {
LNode res = this.head;
while (res.getNext() != pos) {
res = res.getNext();
}
if (res.getNext() == null)
return;
res.setNext(pos.getNext());
}
public void Insert(Polynomial rhs) {
LNode res = this.head;
while (res.getNext() != null && res.getNext().getData().getEps() > rhs.getEps()) {
res = res.getNext();
}
if (res.getNext() == null) {
res.setNext(new LNode(rhs));
return;
}
if (res.getNext().getData().getEps() == rhs.getEps()) {
if (res.getNext().getData().getCof() + rhs.getCof() == 0) {
DeleteLNode(res.getNext());
} else {
res.getNext().getData().setCof(res.getNext().getData().getCof() + rhs.getCof());
}
} else {
LNode temp = new LNode(rhs);
temp.setNext(res.getNext());
res.setNext(temp);
}
}
public void Print() {
LNode res = this.head;
res = res.getNext();
boolean flag = true;
while (res != null) {
if (res.getData().getCof() != 0) {
if (flag == true) {
System.out.print(res.getData().getCof() + " " + res.getData().getEps());
flag = false;
} else {
System.out.print(" " + res.getData().getCof() + " " + res.getData().getEps());
}
}
res = res.getNext();
}
if (flag == true) {
System.out.println("0 0");
return;
}
System.out.print("\n");
}
public void sum(LinkList La, LinkList Lb) {
LNode pa = La.getHead(), pb = Lb.getHead();
pa = pa.getNext();
pb = pb.getNext();
while (pa != null) {
this.Insert(pa.getData());
pa = pa.getNext();
}
while (pb != null) {
this.Insert(pb.getData());
pb = pb.getNext();
}
}
public void multi(LinkList La, LinkList Lb) {
for (LNode pa = La.getHead().getNext(); pa != null; pa = pa.getNext()) {
for (LNode pb = Lb.getHead().getNext(); pb != null; pb = pb.getNext()) {
Polynomial res = pa.getData().PolyMultiplication(pb.getData());
if (res.getCof() == 0) {
continue;
}
this.Insert(res);
}
}
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
LinkList La = new LinkList();
LinkList Lb = new LinkList();
int n;
n = sc.nextInt();
for (int i = 0; i < n; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
La.Insert(new Polynomial(x, y));
}
n = sc.nextInt();
for (int i = 0; i < n; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
Lb.Insert(new Polynomial(x, y));
}
LinkList L_mul = new LinkList();
L_mul.multi(La, Lb);
LinkList L_sum = new LinkList();
L_sum.sum(La, Lb);
L_mul.Print();
L_sum.Print();
}
}
注:这个题卡了一个多小时,应该是一发就过的,但是不知道为什么wa了,后来在计蒜客上跑样例发现Scanner抛出异常,是因为我的读入不是在主函数内(用了initlist函数),后来改成在主函数内就好了。