Java一個難題

The value e^x can be approximated by the sum:1+x+x^2/2!+x^3/3!+......+x^n/n!Write a program that takes a value x as input and outputs this sum for n taken to be each of the values 1 to 10,50,and 100. Your program should repeat the calculation for new values of x until the user says she or he is through. The expression n! is called the factorial of n and is defined asN!=1*2*3*.....*nUse variables of type double to store the factorials (or arrange your calculation toavoid any direct calculation of factorials); otherwise, you are likely to produce inte-geroverflow, that is , integers larger than Java allows.煩請高手代為解答

3 個解答

• 1 0 年前
最佳解答

首先重謄一下題目 The value e^x can be approximated by the sum: 1+x+x^2/2!+x^3/3!+......+x^n/n! Write a program that takes a value x as input and outputs this sum for n taken to be each of the values 1 to 10,50,and 100. Your program should repeat the calculation for new values of x until the user says she or he is through. The expression n! is called the factorial of n and is defined as N!=1*2*3*.....*n Use variables of type double to store the factorials (or arrange your calculation to avoid any direct calculation of factorials); otherwise, you are likely to produce integer overflow, that is , integers larger than Java allows. e^x 的值約等於 1+x+x^2/2!+x^3/3!+......+x^n/n!。 實作一個程式，利用使用者輸入 x 之值，印出當 n = 1 到 10、n = 50 以及 n = 100 時上述算式之值。在使用者確定結束前，你的程式必須能繼續用新的 x 值計算結果。 請用 double 型態來儲存階乘(或調整你的程式來避免直接計算任何階乘)，否則你很有可能會造成整數超載，也就是大於 Java 所能允許的最大整數。以下是我的解法，僅供參考import java.io.*;public class ExponentialFunction { public static void main(String[] args) throws Exception {  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  boolean again = true;  while (again) {   System.out.println();   System.out.print("Please input x: ");   double x = Double.parseDouble(br.readLine());      double[] es = new double[10]; // 儲存 n = 1 到 10 的結果   es[0] = 1 + x;   double ef50 = 1 + x; // n = 50 的結果   double ef100 = 1 + x; // n = 100 的結果   double d = x;   for (int i = 1; i < 100; i++) {    d *= x / (i+1);    if (i < 10) es[i] = es[i-1] + d;    if (i < 50) ef50 += d;    ef100 += d;   }      // 列印結果   System.out.println();   System.out.println("Exponential function of value " + x + " is as followed:");   for (int i = 0; i < 10; i++) {    System.out.println("  Limited to sum a sequence of length " + (i+1) + ": " + es[i]);   }   System.out.println("  Limited to sum a sequence of length 50: " + ef50);   System.out.println("  Limited to sum a sequence of length 100: " + ef100);   System.out.println();   System.out.print("Do you want to continue? (y/n) : ");   String line = br.readLine();   if (line.equals("n")) {    again = !again;   }  } }}

• 1 0 年前

這題不需要高手吧...

• 1 0 年前

意思是

價值 e︿x 能被總數接近：

1+ x ＋x︿ 2/2!＋x︿ 3/3!＋．.....＋x︿ n／n！

寫一個拿價值 x 的計畫當輸入和輸出這一總數對於被拿是的 n

每價值 1 到 10,50,和 100. 你的計畫應該重複計算

對於 x 的新價值直到使用者說她或者他穿越。 表達 n！ 是

呼叫了了 n 的階乘而且被定義當做

N！＝1*2*3*．....＊n

使用類型加倍的變數儲存階乘（或者安排你的計算到

避免任何的指示階乘的計算）; 否則，你可能生產 inte－

geroverflow，也就是說，整數比較大的比較爪哇允許。

參考資料： 翻譯試試看囉