您的位置:首页 > 编程语言 > Java开发

两个多项式相乘和相加的java实现

2017-04-22 01:33 435 查看
PAT上两个多项式相乘和相加的java实现:

import java.util.Scanner;
class Node{
int c;
int e;
Node next;
}
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
Node rear1 = new Node();
Node p1 = rear1;
Node rear2 = new Node();
Node p2 = rear2;
Node pAdd,pMutiply;
int n1 = sc.nextInt();
while(n1--!=0){
int c = sc.nextInt();
int e = sc.nextInt();
rear1 = Attach(c,e,rear1);
}

int n2 = sc.nextInt();
while(n2--!=0){
int c = sc.nextInt();
int e = sc.nextInt();
rear2 = Attach(c,e,rear2);
}
if((n1==0)&&(n2==0)){
System.out.print(0+" "+0);
System.out.println();
System.out.print(0+" "+0);
}else if(n1==0){
System.out.print(0+" "+0);
System.out.println();
print(p2.next);
}else if(n2==0){
System.out.print(0+" "+0);
System.out.println();
print(p1.next);
}else{
pAdd = Add(p1.next,p2.next);
pMutiply = Mutiply(p1.next,p2.next);
print(pMutiply.next);
print(pAdd.next);

}

}
private static Node Mutiply(Node p1, Node p2) {
Node pMutiply1 = new Node();
Node rearMuti = pMutiply1;

Node t2 = p2;
Node t1 = p1;
while(t2!=null){
rearMuti=Attach(t1.c*t2.c, t1.e+t2.e, rearMuti);
t2 = t2.next;
}
t1 = t1.next;
while(t1!=null){
t2 = p2;
rearMuti = pMutiply1;
while(t2!=null){
int e = t1.e + t2.e;
int c = t1.c * t2.c;
while(rearMuti.next!=null&&rearMuti.next.e>e){
rearMuti = rearMuti.next;
}
if((rearMuti.next!=null)&&(rearMuti.next.e==e)){
if((c + rearMuti.next.c)!=0){
rearMuti.next.c = c + rearMuti.next.c;
}else{
Node pp = rearMuti.next;
rearMuti.next = pp.next;
}
}else{
Node temp = new Node();
temp.e = e;
temp.c = c;
temp.next = rearMuti.next;
rearMuti.next = temp;
rearMuti = rearMuti.next;
}
t2 = t2.next;
}
t1 = t1.next;
}
return pMutiply1;
}
private static Node Add(Node p1, Node p2) {
Node tt1 = p1;
Node tt2 = p2;
Node pAdd1 = new Node();
Node rearAdd = pAdd1;
while((tt1!=null)&&(tt2!=null)){
if(tt1.e>tt2.e){
Node temp = create(tt1);
rearAdd.next = temp;
rearAdd = rearAdd.next;
tt1 = tt1.next;
} else if(tt1.e == tt2.e){
if((tt1.c+tt2.c)!=0){
Node temp = new Node();
temp.c = tt1.c+tt2.c;
temp.e = tt1.e;
rearAdd.next = temp;
}
tt1 = tt1.next;
tt2 = tt2.next;
rearAdd = rearAdd.next;
}else{
Node temp = create(tt2);
rearAdd.next = temp;
rearAdd = rearAdd.next;
tt2 = tt2.next;
}
}
while(tt1!=null){
Node temp = create(tt1);
rearAdd.next = temp;
rearAdd = rearAdd.next;
tt1 = tt1.next;
}
while(tt2!=null){
Node temp = create(tt2);
rearAdd.next = temp;
rearAdd = rearAdd.next;
tt2 = tt2.next;
}
return pAdd1;
}

private static Node create(Node p){
Node temp = new Node();
temp.c = p.c ;
temp.e = p.e;
return temp ;
}
private static void print(Node p) {
while(p.next!=null){
System.out.print(p.c+" "+p.e+" ");
p = p.next;
}
System.out.print(p.c+" "+p.e);
System.out.println();

}
public static Node Attach(int cc, int ee, Node rear){
Node pnode = new Node();
pnode.c = cc;
pnode.e = ee;
rear.next = pnode;
rear = pnode;
return rear;
}
}


发现本地调试时没有问题,但是上传PAT测试并不能得到满分,应该是测试用例的还有边界问题在代码中没有考虑到,如果有建议的朋友,可以把问题写在下面。一起交流下。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java