高考申論題
108年
[資訊處理] 程式語言
第 一 題
閱讀以下 Java 程式,列出數學式以說明變數 e 在計算什麼?接著撰寫遞迴(recursive)程式 public static double etx(int accuracy, int x)來計算前述變數 e 的值,撰寫時必須使用 etx 規定的參數與資料型態。(25 分)
import java.util.Scanner;
public class EtoX
{
public static void main( String[] args )
{
Scanner input = new Scanner( System.in );
int number = 1;
int accuracy;
int factorial = 1;
int x;
double e = 1.0;
double exponent = 1.0;
x = input.nextInt();
accuracy = input.nextInt();
while ( number < accuracy )
{
exponent *= x;
factorial *= number;
e += exponent / factorial;
number++;
} // end while loop
System.out.printf( "x: %d%ne: %f%n", x, e );
} // end main
} // end class EtoX
📝 此題為申論題
思路引導 VIP
- 辨識數學模型:首先觀察 while 迴圈內的邏輯。
exponent每次乘以 x(代表 $x^n$),factorial每次乘以 counter(代表 $n!$),兩者相除並加總。這顯然是泰勒級數(Taylor Series)中 $e^x$ 的展開式。 - 遞迴轉換策略:題目要求撰寫遞迴函數
etx。遞迴需要:(A) 終止條件(Base Case),對應迴圈的number < accuracy反向條件;(B) 遞迴規律,考慮如何處理每一項的累加。
🤖
AI 詳解
AI 專屬家教
【考點分析】 本題考察泰勒級數(Taylor Series)的程式實現,以及將迭代(Iteration)邏輯轉換為遞迴(Recursion)的能力。特別是在受限的函數簽章下,如何設計遞迴結束點與回傳值的累加邏輯。 【理論/法規依據】
▼ 還有更多解析內容