高普考題庫
108 年 108年公務人員高等考試三級考試暨普通考試

程式設計概要

本卷皆為申論題,點「看答案與解析」查看擬答。

申論 1Java 程式PreStars 會印出什麼結果?維持巢狀for 迴圈架構,小修PreStars,讓它印出以下的星星構圖。(25 分)2 public class PreStars3 {4 public static void main(String[] args)5 {6 for (int i=1; i<=5; i++) {7 for (int j=1; j<=i; j++)8 System.out.print('*');9 System.out.println();10 }11 }12 }*******************************************************
申論 2下列為Reverse class 的程式規範與其執行結果,試以遞迴(recursive)的方式完成副程式reverse(int[] arr, int x),撰寫時,必須使用相同的參數名稱與資料型態。reverse(int[] arr, int x)會回傳一個倒過來擺置的整數串:arr[n-1], arr[n-2], … arr[x+1], arr[x],假設arr 內共有n 個元素,而且x <= n。(25 分)2 public class Reverse3 {4 public static String reverse(int[] arr, int x)5 {7 }9 public static void main(String[] args)10 {11 int[] intArr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};12 String results = reverse(intArr, 2);13 System.out.println(results);14 System.out.println(reverse(intArr, 7));15 }16 }
申論 3下列Python 程式的執行結果為何?(15 分)list = [2,2,3,7,7,7,9,9,10,10]count = 1current = list[0]for i in list:if i > current :list[count] = icount += 1current = ilast = len(list)if last > count:for i in range(count, last):list.pop()print("count = ", count)print("list = ", list)
申論 4數學中複數是實數的延伸,複數通常表示為a+bi 或(a, b),其中a, b 為實數,分別稱為複數的實部與虛部,i 為虛數單位,且i2=-1。複數的加、減、乘、除運算定義如下:(a+bi)+(c+di) = (a+c)+(b+d)i(a+bi)–(c+di) = (a–c)+(b–d)i(a+bi)*(c+di) = (ac-bd)+(ad+bc)i(a+bi)/(c+di) = ((ac+bd)/(c2+d2))+((bc-ad)/(c2+d2))試參考以下程式回答問題:(35 分)㈠此程式的列印結果為何?㈡利用add(),在ComplexTest.java 中加入一行程式以印出“x + y = (3.0, 3.0)”㈢於Complex.java 中撰寫public Complex division(Complex right)回傳資料型態與參數命名必須分別為Complex 與right。㈣利用division(),在ComplexTest.java中算出y=(2, 2)的倒數(如果y’*y=1則稱 y’為y 的倒數),並列印出有意義的訊息。㈤撰寫public String standardForm()以印出複數的另一表示法a+bi。注意0.0+bi 要表示為bi;a+0.0i 要表示為a;a+1.0i 要表示為a+i。2 public class Complex3 {4 private double real;5 private double imaginary;7 public Complex()8 {9 this(0.0, 0.0);10 }11 public Complex(double r, double i)12 {13 real = r;14 imaginary = i;15 }16 public Complex add(Complex right)17 {18 return new Complex(real + right.real,19 imaginary + right.imaginary);20 }21 public Complex subtract(Complex right)22 {23 return new Complex(real - right.real,24 imaginary - right.imaginary);25 }26 public String toString()27 {28 return String.format("(%.1f, %.1f)", real, imaginary);29 }30 } // end class Complex2 public class ComplexTest3 {4 public static void main(String[] args)5 {6 Complex x = new Complex(1, 1);7 Complex y = new Complex(2, 2);9 System.out.printf("x = %s%n", x.toString());10 System.out.printf("y = %s%n", y);11 }12 } // end class ComplexTest