两个多项式相乘和相加的java实现
来源:互联网 发布:java入门培训费用 编辑:程序博客网 时间:2024/06/05 09:38
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测试并不能得到满分,应该是测试用例的还有边界问题在代码中没有考虑到,如果有建议的朋友,可以把问题写在下面。一起交流下。
0 0
- 两个多项式相乘和相加的java实现
- 使用链表实现两个多项式相加和相乘
- 多项式的相乘和相加
- 两个多项式相加及相乘
- 链表实现的多项式相加相乘
- 数据结构-链表-多项式的相乘和相加
- 数据结构 -- 多项式相加和相乘
- 一元多项式的相加、相乘
- 两个矩阵的相加和相乘
- 数据结构课程设计一元多项式的相加和相乘
- 数据结构实验1(一元多项式的相加和相乘)
- 链表实现多项式相加 相乘
- 链表 实现多项式相加相乘polynomial
- JAVA实现多项式相加
- 【实验】一元多项式的相加与相乘
- 一道 google曾出过的笔试题:编程实现对数学一元多项式的相加和相乘操作
- 大数相加 相乘 java实现
- 重温数据结构:链表的应用之多项式的表示及相加和相乘
- HDU 2215 Maple trees 凸包问题+最小外接圆
- Android(7)消息通信
- Codeforces 798B Mike and strings
- 二.Java关键字
- opencv3.2.XX 配置+windows7(64bit)+VS2015
- 两个多项式相乘和相加的java实现
- RaspberryPI LITE版本默认无SSH及显示屏下连接
- (扩展)欧几里得算法
- 拟牛顿法之L-BFGS算法
- 凸包问题详解(图解)(转载)
- select 语法+别名的使用+连接运算符+distinct+where+like+转义字符+between+in
- 插入排序:直接插入、折半插入排序源码及时间复杂度
- python_python2.7+python3.6安装与并存
- 【HTML】图像标签、文本、表格的颜色及其背景色