導航:首頁 > 編程語言 > java多項式計算

java多項式計算

發布時間:2022-10-23 06:53:58

java求1-1/3+1/5-1/7+……

/*
* 創建日期 2012-4-2
*
* TODO 要更改此生成的文件的模板,請轉至
* 窗口 - 首選項 - Java - 代碼樣式 - 代碼模板
*/

/**
* @author lenovo
*
* TODO 要更改此生成的類型注釋的模板,請轉至
* 窗口 - 首選項 - Java - 代碼樣式 - 代碼模板
*
*/
import java.util.Scanner;
public class Test40006 {
public static void main(String[] args) {
int ri, repeat;
int temp, flag;
double eps, item, sum = 0;
Scanner in=new Scanner(System.in);
repeat=in.nextInt();
for(ri=1; ri<=repeat; ri++){
eps=in.nextDouble();
/*--------------------*/
sum= count(repeat,eps);
/*---------------------*/
System.out.println((int)(sum*10000+0.5)/10000.);
}
}

/**
* @param repeat
* @param eps
* @return s 每一次的運行結果
*/
private static double count(int repeat, double eps) {
double s=0;
//首先計算 1-1/3+1/5-1/7+……
//需要考慮符號和分母變化
long count=0;//項目數 1是第一項 -0.3^3是第二項 ……
count=cishu(eps);//求出項目數
for (long j=1;j<=count;j++){//把一個項目數的多項式的和計算出來
s+=fuhao(j-1,(double)1/(2*j-1));
}
return s;
// TODO 自動生成方法存根

}
/**
* @param eps
* @return m 運行次數 從1開始是第一項,-0.3……3是第二項
*/
private static long cishu(double eps) {
long m=0,n=1;
while(Math.abs((double)1/(2*n-1))>=eps){
m++;n++;
}
return m;
}

/**
* @param count
* @return fu 每一項的值
*/
private static double fuhao(long count,double fumu) {
double fu=0;
fu=Math.pow(-1,count)*fumu;
return fu;
}
}
希望對你有幫助

㈡ 已知一組數據,用JAVA JFRAME利用最小二乘法求出該組數據的多項式擬合公式

/**
* 最小二乘法計算類
*
* @author Administrator
*
*/
public class LeastSquareMethod {
private double[] x;
private double[] y;
private double[] weight;
private int m;

private double[] coefficient;

public LeastSquareMethod(double[] x, double[] y, int m) {
if (x == null || y == null || x.length < 2 || x.length != y.length
|| m < 2)
throw new IllegalArgumentException("無效的參數");
this.x = x;
this.y = y;
this.m = m;
weight = new double[x.length];
for (int i = 0; i < x.length; i++) {
weight[i] = 1;
}
}

public LeastSquareMethod(double[] x, double[] y, double[] weight, int m) {
if (x == null || y == null || weight == null || x.length < 2
|| x.length != y.length || x.length != weight.length || m < 2)
throw new IllegalArgumentException("無效的參數");
this.x = x;
this.y = y;
this.m = m;
this.weight = weight;
}

public double[] getCoefficient() {
if (coefficient == null)
compute();
return coefficient;
}

public double fit(double v) {
if (coefficient == null)
compute();
if (coefficient == null)
return 0;
double sum = 0;
for (int i = 0; i < coefficient.length; i++) {
sum += Math.pow(v, i) * coefficient[i];
}
return sum;
}

private void compute() {
if (x == null || y == null || x.length <= 1 || x.length != y.length
|| x.length < m || m < 2)
return;

double[] s = new double[(m - 1) * 2 + 1];
for (int i = 0; i < s.length; i++) {
for (int j = 0; j < x.length; j++)
s[i] += Math.pow(x[j], i) * weight[j];
}
double[] f = new double[m];
for (int i = 0; i < f.length; i++) {
for (int j = 0; j < x.length; j++)
f[i] += Math.pow(x[j], i) * y[j] * weight[j];
}
double[][] a = new double[m][m];
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
a[i][j] = s[i + j];
}
}
coefficient = Algorithm.multiLinearEquationGroup(a, f);
}

/**
* @param args
*/
public static void main(String[] args) {
LeastSquareMethod l = new LeastSquareMethod(
new double[] { 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 },
new double[] { 37.84, 44.55, 45.74, 63.8, 76.67, 105.59, 178.48, 355.27, 409.92 },
new double[] { 11, 12, 13, 14, 15, 16, 17, 18, 19 },
2);
double[] x = l.getCoefficient();
for (double xx : x) {
System.out.println(xx);
}
System.out.println(l.fit(2009));
}

}

㈢ 如何在JAVA中實現多次方計算

//呵呵,我也是剛剛才學這個OOP程設計的
//規劃的不當,就不要見怪咯,我只是按照我的思想分析
//首先用面向對象的語方分析問題,這里有三個對象
//客戶端是一個對象
//計算面積和周長是一個對象
//長方形是一個對象
public class Client{
public static void main(String []args){
ICalculate cal=new Calculate();
MyRectangle rect=new MyRectangle(10,5);
System.out.println(rect+"周長為:"+cal.calcuGirth(rect));
System.out.println(rect+"面積為:"+cal.calcuArea(rect));
rect=new MyRectangle(30,50);
System.out.println(rect+"周長為:"+cal.calcuGirth(rect));
System.out.println(rect+"面積為:"+cal.calcuArea(rect));

}
}
interface ICalculate{
int calcuArea(MyRectangle rect);
int calcuGirth(MyRectangle rect);
}
class Calculate implements ICalculate{
//計算面積的方法
public int calcuArea(MyRectangle rect){
int result=rect.getWidth()*rect.getHeight();
return result;
}
//計算周長的方法
public int calcuGirth(MyRectangle rect){
int result=(rect.getWidth()+rect.getHeight())*2;
return result;
}
}
class MyRectangle{
private int width;
private int height;
public MyRectangle(int width ,int height){
this.width=width;
this.height=height;
}
public int getWidth(){
return width;
}
public void setWidth(int width){
this.width=width;
}
public void setHeight(int height){
this.height=height;
}
public int getHeight(){
return height;
}
public String toString(){
return super.toString()+"[width="+width+",height="+height+"]";
}
}

編程,輸入一個多項式的項數n,計算下列表達式y=5+55+555+...+555...555

我用java寫的一個...類型用的long 最多隻能到 n只能是19我的想法可能不是很好..只是用個字元串temp記錄,如果輸入的是3. 第一次字元串是5...第二次55...第三次555這樣的..每次結束後 將字元串轉換成long 累加到sum..然後再清空字元串===========================================import java.util.Scanner;
public class nx { public void getY(int num){
long sum = 0; //記錄總和的sum
for (int i = 1; i <= num; i++) {
String temp ="";//記錄每次的變數
for (int j = 0; j < i; j++) {
temp +=5;//每次得到字元
}
sum += Long.parseLong(temp);//轉換後 累加到sum
}
System.out.println(sum);
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("請輸入n:");
int num = in.nextInt();
nx test = new nx();
test.getY(num);
}
}
==================================================運行結果:請輸入n:10
6172839500

㈤ java的crc8校驗,按指定多項式,求助

以下是我的分析,不知是否正確,你參考下1、首先來看你打java代碼:crc=(byte)((crc>>1)^0x8c);和 crc=(byte)(crc>>1); 導致這個問題是因為byte的最高位符號位,轉換的時候就出錯了2、示例代碼:package com.test;public class test {public static void main(String[] args) {byte[] ptr = { 1, 1, 1, 1, 1, 1 };byte res = getCrc(ptr);System.out.println();System.out.println((byte)( (1 >> 1) ^ 0x8c ) + ":" +( (1 >> 1) ^ 0x8c ) );}public static byte getCrc(byte[] ptr) {int crc = 0;for (int i = 0; i > 1) ^ 0x8c;} else {crc = crc >> 1;}}}return (byte) crc;}}

㈥ 計算多項式1!+2!+3!+.......+n!當和超過10000時輸出累加及n的值

#include<stdio.h>

intfactorial(constint&n)
{
if(n==1||n==0)
return1;
returnn*factorial(n-1);
}
intmain()
{
intsum=0,n=1;
intnextSum=0;
while(nextSum<=10000)
{
sum+=factorial(n);
nextSum=sum+factorial(++n);
}
printf("sum=%d,n=%d ",sum,n);
return0;
}

㈦ 求一元n次多項式的值java實現

void AddPolyn(LinkList pa,LinkList pb)//多項式相加 { LinkList ha,hb,void Invert(LinkList L)//逆序輸出鏈表 {LinkList p,q,r; p=L-

㈧ 用Java語言實現求一個矩陣的特徵多項式的程序代碼怎麼寫跪求高人指點~~~

矩陣規模如何?
如果矩陣比較小 (~10), 可以直接按照定義來計算, P[x] = det( x*I - A); 其中det是一個矩陣的行列式; 可以按照定義直接迭代求;

如果矩陣比較大(>100), 可以先求特徵值, 然後構造矩陣特徵多項式 P[x] = (x-x1)*(x-x2)... (x-xn), 其中xi為矩陣的第i個特徵值.

㈨ 我想用java來實現一個 一元多項式的輸入,我定義了一個類polymail 有兩個成員變數 chef 和exp,

請把問題再描述清楚些,給定什麼條件,想要什麼結果?
最好能舉個例子。恐怕很多人對一元多項式對忘了差不多了。

程序已完成。兩個類,一個類Node。封裝多項式用的。一個計算用的。鏈表直接用java裡面的ArraryList了,不用自己再寫結構了。若不知道怎麼運行java類,請補充問題。
效果如下:

請輸第1個項多式以回車結束: 格式如:2^0+8^3+7^5+6^6+7^9
2^0+8^3+7^5+6^6+7^9
請輸第2個項多式以回車結束:
3^6+4^3+5+2^8+6^2
=======以下為輸出結果======
多項式1:2+8x^3+7x^5+6x^6+7x^9
多項式2:5+6x^2+4x^3+3x^6+2x^8
多項式和:7+6x^2+12x^3+7x^5+9x^6+2x^8+7x^9

程序如下:
文件 Node.java
public class Node implements Comparable<Node>{
private int xishu;//系數
private int shu; //指數

public int getXishu() {
return xishu;
}

public void setXishu(int xishu) {
this.xishu = xishu;
}

public int getZhishu() {
return shu;
}

public void setZhishu(int shu) {
this.shu = shu;
}

public Node(){

}

public Node(int a,int b){
xishu=a;
shu=b;
}

/**
* 通過指數來比較整個對象大小
*/
public int compareTo(Node node) {
return this.shu-node.shu;
}

/**
* 當 系統與指數都相等時,說明整個對象都相等.
*/
@Override
public boolean equals(Object o) {
Node node = (Node) o;
if(this.shu==node.shu&&this.xishu==node.xishu){
return true;
}else{
return false;
}
}

}

文件: Test.java

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

/**
* @author Youngao
* @Time 2011-03-18
* @Relue 多項式加法規則:對於兩個多項式中指數相同的子項,其系數相加,若系數的和非零,
* 則構成「和多項式」中的一項;對於指數不同的項,直接構成「和多項式」中的一項。
*/
public class Test {

public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
System.out.println("請輸第1個項多式以回車結束: 格式如:2^0+8^3+7^5+6^6+7^9");
String str1 = stdin.nextLine();
List<Node> list1 = splitStr(str1);
System.out.println("請輸第2個項多式以回車結束: ");
String str2 = stdin.nextLine();
List<Node> list2 = splitStr(str2);
System.out.println("=======以下為輸出結果======");
printPolyn("多項式1:",list1);
printPolyn("多項式2:",list2);
//相加
List<Node> newlist = AddPolyn(list1,list2);
printPolyn("多項式和:",newlist);
}

/**
* 兩一元多項式相加
*/
private static List<Node> AddPolyn(List<Node> list1, List<Node> list2) {
List<Node> deletenode1 = new ArrayList<Node>();
List<Node> deletenode2 = new ArrayList<Node>();
List<Node> newlist = new ArrayList<Node>();
for(int i=0;i<list1.size();i++){
Node list1node = list1.get(i);
for(int j=0;j<list2.size();j++){
Node list2node = list2.get(j);
//憑規則 重新組裝 '和結果'的鏈表
if(list1node.getZhishu()==list2node.getZhishu()){
Node newnode = new Node();
newnode.setXishu(list1node.getXishu()+list2node.getXishu());
newnode.setZhishu(list1node.getZhishu());
deletenode1.add(list1node);
deletenode2.add(list2node);
newlist.add(newnode);
break;
}
}
}
//去除已被求和的各項
list1.removeAll(deletenode1);
list2.removeAll(deletenode2);
//拼接剩餘所有項
for(int i=0;i<list2.size();i++){
Node list2node = list2.get(i);
if(!newlist.contains(list2node)){
newlist.add(list2node);
}
}
for(int i=0;i<list1.size();i++){
Node list1node = list1.get(i);
if(!newlist.contains(list1node)){
newlist.add(list1node);
}
}
// 按指數從低到高排序
Collections.sort(newlist);
return newlist;
}

/**
* 統一的列印方法
*/
private static void printPolyn(String str, List<Node> list) {
String result = str;
for(int i=0;i<list.size();i++){
Node node = list.get(i);
if(i==list.size()-1){
if(node.getZhishu()==0){
result += node.getXishu()+"";
}else{
result += node.getXishu()+"x^"+node.getZhishu();
}
}else{
if(node.getZhishu()==0){
result += node.getXishu()+"+";
}else{
result += node.getXishu()+"x^"+node.getZhishu()+"+";
}
}
}
System.out.println(result);
}

/**
* 分割字元串,並封裝鍵表
*/
private static List<Node> splitStr(String str1) {
String[] strs = str1.split("\\+");
List<Node> list = new ArrayList<Node>();
for (int i = 0; i < strs.length; i++) {
String[] subnode = strs[i].split("\\^");
int xishu = Integer.valueOf(subnode[0].trim());
int shu = 0;
if(subnode.length>1)
shu = Integer.valueOf(subnode[1].trim());
Node node = new Node(xishu,shu);
list.add(node);
}
// 按指數從低到高排序
Collections.sort(list);
return list;
}

}

㈩ java語言用鏈表實現多項式減法代碼 運行結果總是不對!求大神~

對於多項式減法,不是很了解,不過 你先把b的coef設為負數,然後再進行比較,這樣的比較結果還會正確嗎?

閱讀全文

與java多項式計算相關的資料

熱點內容
ai文件pdf 瀏覽:909
騰訊雲伺服器掛載混合雲 瀏覽:758
智能小車用什麼單片機 瀏覽:463
java怎麼給窗口關閉 瀏覽:940
列舉51單片機的定址方式 瀏覽:706
剪輯app怎麼寫長篇文字 瀏覽:400
app專屬流量過月租怎麼不更新 瀏覽:654
王者程序員都有誰 瀏覽:76
給牛換腳掌解壓 瀏覽:387
圍棋有多少種演算法 瀏覽:602
unity資源包在哪個文件夾 瀏覽:704
阿里雲伺服器遠程鏈接不成功 瀏覽:482
文件系統pdf 瀏覽:766
原神安卓區服什麼意思 瀏覽:37
貝殼app怎麼線上發布 瀏覽:159
如何挑選安卓系統機頂盒 瀏覽:54
安卓快充使用有什麼注意事項 瀏覽:909
黑馬程序員的雲計算網課 瀏覽:947
endnotestyle文件夾怎麼導入 瀏覽:460
講解少兒編程演講會開頭 瀏覽:426