java list想加_利用java List 实现多项式相加,相乘
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 La = init();
List Lb = init();
System.out.println(polyMulti(La,Lb));
}
private static List init() {
List poly = new LinkedList();
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 polyPlus(List La,List Lb){
List 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 polyPlus_update(List La,List 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 polyMulti(List La,List Lb){
List 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 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() ;
}
}
總結
以上是生活随笔為你收集整理的java list想加_利用java List 实现多项式相加,相乘的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java控制语句案例_Java基础语法—
- 下一篇: java左手画圆右手画方_左手画圆,右手