1定义类Term这个类将定义多项式的项。它应该有两个属性,系数和项的指数。
2.要使用LinkedList
3.使用toString 输出
4.使用scanner 输入输出
java 使用LinkedList 实现多项式的相加相乘
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- 蔡能教授,网站特聘专家 2019-07-18 07:39关注
package com.learn.algorithm.ploy; import java.util.LinkedList; import java.util.List; import java.util.Scanner; /** *多项式 相关 运算 */ public class Ploy { public static void main(String[] args) { List<Node> La = init(); List<Node> Lb = init(); System.out.println(polyMulti(La,Lb)); } private static List<Node> init() { List<Node> poly = new LinkedList<Node>(); Scanner sc = new Scanner(System.in); System.out.println("请输入 系数和参数(例如 a,b 表示 aX^b,输入 0,0 结束。):"); while (true) { String line = sc.nextLine(); if ( vaildate(line) ){ String[] split = line.split(","); int coefficient = Integer.parseInt(split[0]); int exponential = Integer.parseInt(split[1]); if(coefficient == 0 && exponential == 0){ break; } poly.add(new Node(coefficient, exponential)); } else { System.out.println("[" + line + "]输入有误"); } } System.out.println(poly); return poly; } /** * 多项式加法 * @param La * @param Lb * @return */ public static List<Node> polyPlus(List<Node> La,List<Node> Lb){ List<Node> Lc = new LinkedList<>(); int Sa = La.size(),Sb=Lb.size(),ia=0,ib=0; while( ia < Sa && ib < Sb ){ if ( La.get(ia).getExponential()< Lb.get(ib).getExponential()){ Lc.add(La.get(ia)); ia ++ ; } else if( La.get(ia).getExponential() == Lb.get(ib).getExponential() ){ int coe = La.get(ia).getCoefficient() + Lb.get(ib).getCoefficient(); if( coe != 0 ){ Lc.add(new Node(coe,La.get(ia).getExponential())); } ia ++ ; ib ++; } else { Lc.add(Lb.get(ib)); ib ++; } } while (ia < Sa) { Lc.add( La.get(ia++) ); } while (ib < Sb) { Lc.add( Lb.get(ib++) ); } return Lc ; } /** * 多项式加法(无序) * @param La * @param Lb * @return */ public static List<Node> polyPlus_update(List<Node> La,List<Node> Lb){ int Sa = La.size(),Sb=Lb.size(),ia=0,ib=0; while( ia < Sa ){ Node node = La.get(ia); Node nodeByExp = getNodeByExp( Lb,node.getExponential() ); if (nodeByExp != null) { if (node.getCoefficient() + nodeByExp.getCoefficient() == 0) { Lb.remove(nodeByExp); } else{ nodeByExp.setCoefficient( node.getCoefficient() + nodeByExp.getCoefficient() ); } } else { Lb.add(node); } ia ++; } return Lb ; } /** * 多项式乘法 * @param La * @param Lb * @return */ public static List<Node> polyMulti(List<Node> La,List<Node> Lb){ List<Node> Lc = new LinkedList<>(); int Sa = La.size(),Sb=Lb.size(),ia=0,ib=0; while( ia < Sa ){ ib = 0; Node Na = La.get(ia); while (ib < Sb) { Node Nb = Lb.get(ib); int exp = Nb.getExponential() + Na.getExponential();//指数相加 int coe = Nb.getCoefficient() * Na.getCoefficient();//系数相乘 Node nodeByExp = getNodeByExp( Lc, exp); if (nodeByExp != null) { if (coe + nodeByExp.getCoefficient() == 0) { Lc.remove(nodeByExp); } else{ nodeByExp.setCoefficient( coe+ nodeByExp.getCoefficient() ); } } else { Lc.add(new Node(coe,exp)); } ib ++ ; } ia ++; } return Lc ; } /** * 根据系数 寻找对应的项,没有则返回null * @param p * @param exp * @return */ public static Node getNodeByExp(List<Node> p,Integer exp){ if (exp == null || p == null ){ return null; } for (Node node : p) { if (node.exponential == exp) { return node; } } return null; } /** * 验证输入字符串的合法性 * @param s * @return */ public static boolean vaildate(String s){ return s.matches("[-]{0,1}[0-9]+[,]{1}[0-9]+"); } } /**实体类 * */ class Node { Integer coefficient =0; //系数 Integer exponential =0; //指数 public Node(Integer coefficient,Integer exponential){ this.coefficient = coefficient; this.exponential = exponential; } public Integer getCoefficient() { return coefficient; } public void setCoefficient(Integer coefficient) { this.coefficient = coefficient; } public Integer getExponential() { return exponential; } public void setExponential(Integer exponential) { this.exponential = exponential; } @Override public String toString() { return this.coefficient.toString() + "X^" + this.exponential.toString() ; } }
解决 无用评论 打赏 举报
悬赏问题
- ¥30 这是哪个作者做的宝宝起名网站
- ¥60 版本过低apk如何修改可以兼容新的安卓系统
- ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
- ¥50 有数据,怎么建立模型求影响全要素生产率的因素
- ¥50 有数据,怎么用matlab求全要素生产率
- ¥15 TI的insta-spin例程
- ¥15 完成下列问题完成下列问题
- ¥15 C#算法问题, 不知道怎么处理这个数据的转换
- ¥15 YoloV5 第三方库的版本对照问题
- ¥15 请完成下列相关问题!