日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java list想加_利用java List 实现多项式相加,相乘

發布時間:2024/9/3 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 实现多项式相加,相乘的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。