108年 高普考 普通考試 資訊處理 程式設計概要 試卷

pdf
76.25 KB
4 頁
win7 2007
侵權投訴
加載中. ..
PDF
108年公務人員普通考試試題
科:資訊處理
目:程式設計概要
考試時間1小時 30 座號:
※注意: 禁止使用電子計算器。
不必抄題,作答時請將試題題號及答案依照順序寫在試卷上,於本試題上作答者,不予計分。
本科目除專門名詞或數理公式外,應使用本國文字作答。
代號:44550
頁次:4
1
一、Java 程式 PreStars 會印出什麼結果?維持巢狀 for 迴圈架構,小修
PreStars,讓它印出以下的星星構圖。25 分)
1
2 public class PreStars
3 {
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(&apos;*&apos;);
9 System.out.println();
10 }
11 }
12 }
**********
*********
********
*******
******
*****
****
***
**
*
代號:44550
頁次:4
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 <= n25 分)
2 public class Reverse
3 {
4 public static String reverse(int[] arr, int x)
5 {
6
7 }
8
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 }
10 9 8 7 6 5 4 3
10 9 8
三、下列 Python 程式的執行結果為何?(15 分)
list = [2,2,3,7,7,7,9,9,10,10]
count = 1
current = list[0]
for i in list:
if i > current :
list[count] = i
count += 1
current = i
last = len(list)
if last > count:
for i in range(count, last):
list.pop()
print("count = ", count)
print("list = ", list)
代號:44550
頁次:4
3
四、數學中複數是實數的延伸複數通常表示為 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 divisionComplex right
回傳資料型態與參數命名必須分別為 Complex right
利用division()ComplexTest.java中算出y=(2, 2)的倒數(如果y’*y=1
則稱 y’y的倒數),並列印出有意義的訊息。
撰寫 public String standardForm()以印出複數的另一表示法 a+bi。注意
0.0+bi 要表示為 bia+0.0i 要表示為 aa+1.0i 要表示為 a+i
2 public class Complex
3 {
4 private double real;
5 private double imaginary;
6
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);
代號:44550
頁次:4
4
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 Complex
2 public class ComplexTest
3 {
4 public static void main(String[] args)
5 {
6 Complex x = new Complex(1, 1);
7 Complex y = new Complex(2, 2);
8
9 System.out.printf("x = %s%n", x.toString());
10 System.out.printf("y = %s%n", y);
11 }
12 } // end class ComplexTest
收藏 ⬇️ 下載