iT邦幫忙

0

用JAVA寫數學問題

  • 分享至 

  • xImage

最近練習用JAVA寫了一些數學式,像是費氏級數
有關費氏級數大家可以去科普一下
在數學上,斐波那契數是以遞迴的方法來定義:
用文字來說,就是費氏數列由0和1開始,之後的斐波那契數就是由之前的兩數相加而得出。首幾個斐波那契數是:
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377 ,610, 987……

程式碼大概是這樣


public class Fibonacci {

	public static void main(String[] args) throws IOException {
		int num;
		String  str;
		BufferedReader buf;
		buf=new BufferedReader(new InputStreamReader(System.in));
		System.out.println("使用遞迴計算費氏級數\n");
		System.out.println("請輸入一個整數:");
		str=buf.readLine();
		num = Integer.parseInt(str);
		if (num<0)
			System.out.print("輸入數字必須大於0\n");
		else
			System.out.print("Fibonacci("+num+")="+Fibonacci(num)+"\n");
	}
	public static int Fibonacci(int n) 
	{
		if(n==0)//第0項為0
			return (0);
		else if(n==1)//第1項為1
	return (1);
		else
			return(Fibonacci(n-1)+Fibonacci(n-2));
	}

}

有關費氏級數的程式比較沒問題
現在想利用JAVA寫出多項式表達(在大陸稱為韋達定理)
有興趣這數學式的可以參考李永樂老師,我覺得他講的數學問題都很棒
https://www.youtube.com/watch?v=qVANpN_wNy4
目前有幾項多項式的JAVA題目,想和大家討論看看做法

/** Constructor from an array of coefficients c[] where c[i] is the coefficient of the x^i term */
public Polynomial(int[] coef) { }

/** returns the power of the highest-order non-zero term. */
public int degree() { }

/** returns a string representation of the polynomial (use 
"x" as the dummy variable and format from the low-order to high-order powers */
/** Your string should be in the following form:
  * x+2x^2-4x^5+x^6
  *
  * NOTE: if your string does not follow the above format, the provided
  * parser functions will not work for you!
  */
public String toString() { }

/** operations on Polynomials returns this + b */
/** 
  * Mathematical expressions of the answer should be
  *    (a0+b0) + (a1+b1)*x + (a2+b2)*x^2 + (a3+b3)*x^3 + ... + (an+bn)*x^n
  */
public Polynomial add(Polynomial b) { } 

/** returns this - b */
/** 
  * Mathematical expressions of the answer should be
  *    (a0-b0) + (a1-b1)*x + (a2-b2)*x^2 + (a3-b3)*x^3 + ... + (an-bn)*x^n
  */
public Polynomial sub(Polynomial b) { } 

/** returns this * b*/
/** 
  * Mathematical expressions of the answer should be
  *    (a0*b0) + (a0*b1+a1*b0)*x + (a0*b2+a1*b1+a2*b0)*x^2 +
*    (a0*b3+a1*b2+a2*b1+a3*b0)*x^3 + ... + (an*bn)*x^2n
  */
public Polynomial mul(Polynomial b)  { }

/** returns this * c */
/** 
  * Mathematical expressions of the answer should be
  *    (c*a0) + (c*a1)*x + (c*a2)*x^2 + (c*a3)*x^3 + ... + (c*an)*x^n
  */
public Polynomial scale(int c)  { }

我的答案下篇或是回覆寫給大家看,當然可能不一定對的,歡迎大家挑戰看看

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答