• 沒有找到結果。

Section A1

N/A
N/A
Protected

Academic year: 2021

Share "Section A1"

Copied!
17
0
0

加載中.... (立即查看全文)

全文

(1)

Format 類型 # Questions 題目數 Total Marks

Section A1 部 (一) True or False 真假題 5 5

Section A2 部 ( ) Multiple Choice 多項選擇題 20 20

Section B Fill-in-the-blanks 填充題 8 (A - L) 20

Total 總分 45

(1) Assume that all variables without declaration shown in the following program segments have already been declared properly as 32-bit signed integers (Pascal:longint, C / C++: int).

下列程序段中所有未有列出宣告的變量,均假設已經適當地宣告為 32 位元有符號的整數 (Pascal:

longint, C / C++:int)。

(2) The following code is added to the beginning of all C and C++ programs.

在所有 C 和 C++ 程序的頂部加入以下程式碼:

C

#include <stdio.h>

#include <string.h>

#include <math.h>

#include <stdlib.h>

#include <stdbool.h>

C++

#include <cstdio>

#include <cmath>

#include <cstdlib>

#include <string>

#include <iostream>

using namespace std;

For C,stdbool.hdefines the boolean data type booland values true(equivalent to 1) and false (equivalent to 0).

對於 C,stdbool.h定義了布爾數據類型bool及值true(等同 1) 及false(等同 0)。

(3) Assume all programs are compiled properly in Ubuntu 16.04 using the compilers and commands below.

假設所有程序都在 Ubuntu 16.04 下使用以下編譯器及指令正確地編譯。

Pascal: Free Pascal (fp-compiler3.0.0) fpc program.pas

C: GNU GCC (gcc-4.94.9.3) gcc -std=c99 program.c -o program C++: GNU G++ (g++-4.94.9.3) g++ -std=c++98 program.cpp -o program

(2)

Section A1

部 (一) (5 marks 分)

For each question, determine whether the statement is true or false, then mark the corresponding box (T: true or F: false) on the answer sheet. One mark for each correct answer. No marks will be deducted for wrong answers.

請判斷下列每題的陳述句的真假,然後把答題紙對應的空格 (T: 真或 F: 假) 填滿。答對得一分,答錯不 扣分。

1. The size of a character (char) variable is the same as that of a boolean (Pascal: boolean, C/C++: bool) variable.

一個字符 (char) 變量的大小與一個布爾 (Pascal: boolean, C/C++:bool) 變量的大小相同。

2. It is possible to write a program without using any IDE (Integrated Development Environment) 我們可以在沒有使用任何 IDE(整合開發環境) 的情況下編寫一個程式。

3. The following program can be compiled successfully.

以下的程序能被成功編譯。

Pascal

var

a: array[0..9] of double;

begin

read(a[0.5]);

write(a[0.5] + 1) end.

C

double a[10];

int main() {

scanf("%lf", &a[0.5]);

printf("%f", a[0.5] + 1);

return 0;

}

C++

double a[10];

int main() { cin >> a[0.5];

cout << a[0.5] + 1;

return 0;

}

4. We can always sort an array of N elements with at most⌈0.5N⌉ swaps (not necessarily consecutive ones), where

⌈x⌉ is the least integer not less than x.

我們一定可以用不多於⌈0.5N⌉ 次交換(不必須為相鄰的)來排序一段大小為 N 的陣列,當中 ⌈x⌉ 表示

不小於 x 的最小整數。

5. The HTTP status code 404 means that there is an internal server error.

HTTP 狀態碼 404 是指出現了內部伺服器錯誤。

(3)

Section A2

部 ( ) (20 marks 分)

For each question, choose the most appropriate answer and mark the corresponding box (A, B, C, or D) on the answer sheet. One mark for each correct answer. No marks will be deducted for wrong answers.

請為下列每題各選一個最適合的答案,然後把答題紙對應的空格(A、B、C、或 D)填滿。

答對得一分,答錯不扣分。

6. A competitive programming club has 10 members. How many ways are there to choose, among the members, one president and two vice presidents?

一個編程學會有 10 位會員。有多少種方法從會員中選出一位主席和兩位副主席?

A. 90 B. 180 C. 360 D. 720

7. Which of the following character sets does not support Chinese characters?

以下哪個字符集不支援中文字符?

A. ASCII B. Big5 C. UTF-8 D. UTF-16

8. Alice has an integer between -16 and 16 (inclusive) in her mind. You can ask her some questions, each of which must be in the form of “is your number strictly less thanx?”(xcan be any real number). You can decide the question you are going to ask according to the answers of previous questions. In the worst case, at least how many questions should be asked to know the exact value of the integer Alice has in mind?

愛麗絲心中有一個介乎 -16 和 16 之間(含)的整數。你可以詢問她一些問題,每條問題都必須跟隨以

下格式:「你所想的數字是嚴格小於 x的嗎?」(x可以是任意實數)。你可以根據之前的結果決定即將

要詢問的問題。在最壞情況下,最少需要詢問多少個問題以準確得知愛麗絲心中的數字?

A. 4 B. 5 C. 6 D. 7

(4)

9. If Charlie gets full marks in the final exam, he feels happy; if Charlie feels happy and it is raining outside, he plays computer games at home. Charlie is not playing computer games at home. Which of the followings must be true?

如果查理在考試中取得滿分,他會感到開心;如果查理感到開心且外面正在下雨,他會留在家中玩遊 戲機。查理並不是留在家中玩遊戲機。以下哪項必為正確?

A. Charlie does not get full marks in the final exam 查理未有在考試中取得滿分

B. Charlie does not feel happy 查理不感到開心

C. It is not raining outside 外面沒有下雨

D. None of the above 以上皆非

10. Consider the following program: 考慮以下程序:

Pascal

var x, a, b: longint;

begin read(x);

a := (x - 1) div 2;

b := (x + 1) div 2;

if (a < b) then write('<');

if (a = b) then write('=');

if (a > b) then write('>') end.

C

int x, a, b;

int main() { scanf("%d", &x);

a = (x - 1) / 2;

b = (x + 1) / 2;

if (a < b) printf("<");

if (a == b) printf("=");

if (a > b) printf(">");

return 0;

}

C++

int x, a, b;

int main() { cin >> x;

a = (x - 1) / 2;

b = (x + 1) / 2;

if (a < b) cout << "<";

if (a == b) cout << "=";

if (a > b) cout << ">";

return 0;

}

Assume that inputxis an integer between -1024 and 1023 inclusive. Which of the followings is/are possible output(s)?

假設輸入x是個 -1024 和 1023 之間(含)的整數。以下哪些是可能的輸出?

i. <=

ii. <

iii. = iv. >

A. i only 只有 i

B. i and iv only 只有 i 和 iv C. ii and iii only 只有 ii 和 iii D. ii, iii and iv only 只有 ii、iii 和 iv

(5)

11. What is the output of the following program? 以下程序的輸出是什麼?

Pascal

var

x: longint = 123;

begin

x := x div 10 + 100 * (x mod 10);

write(x) end.

C

int x = 123;

int main() {

x = x / 10 + 100 * (x % 10);

printf("%d", x);

return 0;

}

C++

int x = 123;

int main() {

x = x / 10 + 100 * (x % 10);

cout << x;

return 0;

}

A. 231 B. 312 C. 321 D. 123

12. Four players, namely W, X, Y and Z, are randomly assigned into two pairs. For each pair, the two players play a single match. The winners from these two matches play against each other in the final match.

四位玩家:W、X、Y 和 Z 被隨機分配至兩對。對於每一對,兩位玩家會進行一場比賽。這兩場比賽的 勝方會再進行一場決賽。

It is given the outcomes of all possible matches:

已知所有可能對陣的結果:

1. In a match between W and X, W always win. W 和 X 之間的比賽,W 必勝。

2. In a match between W and Y, Y always win. W 和 Y 之間的比賽,Y 必勝。

3. In a match between W and Z, Z always win. W 和 Z 之間的比賽,Z 必勝。

4. In a match between X and Y, Y always win. X 和 Y 之間的比賽,Y 必勝。

5. In a match between X and Z, X always win. X 和 Z 之間的比賽,X 必勝。

6. In a match between Y and Z, Z always win. Y 和 Z 之間的比賽,Z 必勝。

Which of the following pairs is/are possible result(s) in the final match?

以下哪些決賽結果是有可能發生的?

Winner 勝方 Loser 敗方

i. W Z

ii. Y X

iii. Y W

A. ii only 只有 ii

B. iii only 只有 iii C. ii and iii only 只有 ii 和 iii D. i, ii and iii i、ii 和 iii

(6)

13. Consider the following function: 考慮以下函數:

Pascal

function same(a, b: longint): longint;

begin

if (  X  ) then same := 1 else

same := 0 end;

C / C++

int same(int a, int b) { if (  X  )

return 1;

else return 0;

}

Which of the followings can be filled in X such that the function returns 1 if and only if a = b ? 以下哪些可以代入 X,令以上函數當且僅當 a = b 時才會傳回 1?

i.

ii.

Pascal

(a or b) = (a and b)

(a xor b) = 0

C / C++

(a | b) == (a & b)

(a ^ b) == 0

A. None of the above 以上皆否

B. i only 只有 i

C. ii only 只有 ii

D. i and ii i 和 ii

14. What is the output of the following program? 以下程序的輸出是什麼?

Pascal

var

s: string = 'hkoi201718';

temp: char;

i: longint;

arr: array[1..6] of longint = (1, 4, 0, 3, 5, 2);

begin

for i := 1 to 6 do begin

s[i + arr[i]] := s[i + 1];

temp := s[i + arr[i]];

s[i + 1] := temp end;

write(s) end.

C

char s[11] = "hkoi201718";

char temp;

int i;

int arr[6] = {1, 4, 0, 3, 5, 2};

int main() {

for (i = 0; i <= 5; i++) { s[i + arr[i]] = s[i + 1];

temp = s[i + arr[i]];

s[i + 1] = temp;

}

printf("%s", s);

return 0;

}

C++

string s = "hkoi201718";

char temp;

int i;

int arr[6] = {1, 4, 0, 3, 5, 2};

int main() {

for (i = 0; i <= 5; i++) { s[i + arr[i]] = s[i + 1];

temp = s[i + arr[i]];

s[i + 1] = temp;

}

cout << s;

return 0;

}

A. hki018721o B. ho2011i7k8 C. hooikii7k8 D. hkii2o221o

(7)

15. Consider the following program: 考慮以下程序:

Pascal

var

a, p: array[0..7] of longint;

i: longint;

begin

for i := 0 to 7 do read(a[i]);

p[0] := a[0];

for i := 1 to 7 do

p[i] := p[i - 1] + a[i];

write((p[7] - p[6])

* (p[4] - p[2])) end.

C

int a[8], p[8];

int i;

int main() {

for (i = 0; i <= 7; i++) scanf("%d", &a[i]);

p[0] = a[0];

for (i = 1; i <= 7; i++) p[i] = p[i - 1] + a[i];

printf("%d", (p[7] - p[6])

* (p[4] - p[2]));

return 0;

}

C++

int a[8], p[8];

int i;

int main() {

for (i = 0; i <= 7; i++) cin >> a[i];

p[0] = a[0];

for (i = 1; i <= 7; i++) p[i] = p[i - 1] + a[i];

cout << (p[7] - p[6])

* (p[4] - p[2]);

return 0;

}

On which of the following inputs does the program output the largest number?

以下哪一組輸入會生成最大的輸出?

A. 3 1 4 1 5 9 2 6 B. 5 3 5 8 9 7 9 3 C. 2 3 8 4 6 2 6 4 D. 3 3 8 3 2 7 9 5

16. What is the output of the following program? 以下程序的輸出是甚麼?

Pascal

var

a: array[0..199] of longint;

i, j: longint;

sum: longint = 0;

begin

for i := 20 downto 1 do a[i] := i;

for i := 20 downto 1 do begin

for j := 1 to a[i] do

a[i - 1] := a[i - 1] div 2;

sum := sum + a[i]

end;

write(sum) end.

C

int a[200];

int i, j;

int sum = 0;

int main() {

for (i = 20; i >= 1; i--) a[i] = i;

for (i = 20; i >= 1; i--) { for (j = 1; j <= a[i]; j++)

a[i - 1] = a[i - 1] / 2;

sum = sum + a[i];

}

printf("%d", sum);

return 0;

}

C++

int a[200];

int i, j;

int sum = 0;

int main() {

for (i = 20; i >= 1; i--) a[i] = i;

for (i = 20; i >= 1; i--) { for (j = 1; j <= a[i]; j++)

a[i - 1] = a[i - 1] / 2;

sum = sum + a[i];

}

cout << sum;

return 0;

}

A. 19 B. 55 C. 110 D. 210

(8)

17. Consider the following program: 考慮以下程序:

Pascal

var

a, b, x: longint;

begin

read(a, b);

x := a div 2;

a := a - x;

b := b + x;

x := b div 4;

a := a + x;

b := b - x;

write(a, ' ', b) end.

C

int a, b, x;

int main() {

scanf("%d %d", &a, &b);

x = a / 2;

a = a - x;

b = b + x;

x = b / 4;

a = a + x;

b = b - x;

printf("%d %d", a, b);

return 0;

}

C++

int a, b, x;

int main() { cin >> a >> b;

x = a / 2;

a = a - x;

b = b + x;

x = b / 4;

a = a + x;

b = b - x;

cout << a << " " << b;

return 0;

}

Given that the output of the above program is30 30, which of the followings can be the program’s input?

已知上述程序的輸出是 30 30,以下哪項可能是程序的輸入?

A. 35 25 B. 39 21 C. 44 16 D. 46 14

18. What is the output of the following program?

以下程序的輸出是甚麼?

Pascal

function f(): longint;

var res, x: longint;

begin res := 0;

x := 2018;

while (x > 0) do begin

res := res + x mod 10;

x := x div 10 end;

f := res end;

C / C++

int f() { int res, x;

res = 0;

x = 2018;

while (x > 0) { res = res + x % 10;

x = x / 10;

}

return res;

}

A. 2 B. 8 C. 11 D. 15

(9)

19. Consider the following program segment: 考慮以下程序段:

Pascal

var

a: array[0..99] of longint;

p: longint;

procedure f(x: longint);

begin a[p] := x;

inc(p) end;

function g(): longint;

begin dec(p);

g := a[p]

end;

C / C++

int a[100];

int p;

void f(int x) { a[p] = x;

p++;

}

int g() { p--;

return a[p];

}

Which of the following data structures does the above program segment implement?

以上程序段實現了以下哪一項數據結構?

A. Queue 隊列

B. Cyclic queue (circular buffer) 循環隊列(圓形緩衝區)

C. Stack 棧

D. Linked list 鏈表

20. Consider the following program: 考慮以下程序:

Pascal

var

x, y: longint;

begin

read(x, y);

if (x div 2 <= y div 2) then write('a');

if (x div 3 = y div 3) then write('b');

if (x div 5 >= y div 5) then write('c')

end.

C

int x, y;

int main() {

scanf("%d %d", &x, &y);

if (x / 2 <= y / 2) printf("a");

if (x / 3 == y / 3) printf("b");

if (x / 5 >= y / 5) printf("c");

return 0;

}

C++

int x, y;

int main() { cin >> x >> y;

if (x / 2 <= y / 2) cout << "a";

if (x / 3 == y / 3) cout << "b";

if (x / 5 >= y / 5) cout << "c";

return 0;

}

Assume inputsxandyare integers between 1 and 100 inclusive. Which of the followings are possible outputs of the program above?

假設輸入xy分別是 1 和 100 之間(含)的整數。以下哪些是有可能的輸出?

i. ab ii. ac iii. bc

A. i and ii only 只有 i 和 ii B. i and iii only 只有 i 和 iii C. ii and iii only 只有 ii 和 iii D. i, ii and iii i、ii 和 iii

(10)

21. Palindromes are strings that read the same forward or reversed. For example, “ABBA” is a palindrome while

ABCD” isn’t.

迴文是正讀反讀都一樣的字串。舉例說,“ABBA” 是迴文,而 “ABCD” 則不是。

Which of the following strings can be re-ordered to form a palindrome?

以下哪些字串可以重組成迴文?

i. BCBCCCAACA ii. ZZYZXYX iii. RQSPSPQRPSPS

A. i only 只有 i

B. ii only 只有 ii

C. i and iii only 只有 i 和 iii D. ii and iii only 只有 ii 和 iii

22. A string s[1...L] of length L is said to be periodic, if there exists a positive divisor P of L, such that P < L and s[i + P ] = s[i] for 1≤ i ≤ L − P .

對於一個長度為 L 的字串 s[1...L],當存在一個 L 的正因數 P 使得 P < L 且對於 1 ≤ i ≤ L − P , s[i + P ] = s[i],我們稱這個字串為週期性的。

Which of the following strings can be re-ordered to form a periodic string?

以下哪些字串可以重組成週期性的字串?

i. ABACCABAB ii. HKOIOHIK iii. IOIPPPPPPIOI

A. i only 只有 i

B. ii only 只有 ii

C. i and iii only 只有 i 和 iii D. ii and iii only 只有 ii 和 iii

23. Consider the following function: 考慮以下函數:

Pascal

function f(n: longint): longint;

begin

while (n mod 2 = 0) do n := n div 2;

f := n end;

C / C++

int f(int n) { while (n % 2 == 0)

n = n / 2;

return n;

}

Which of the followings returns the largest value?

以下哪項傳回最大值?

A. f(65) B. f(122) C. f(4032)

(11)

24. Leta[0..999]be an integer array sorted in ascending order.

a[0..999]為一個由小至大排序好的整數陣列。

In array a, given that there exists a value x that appears more than 500 times, at least how many element(s) need(s) to be accessed to obtain its value?

在陣列a中,已知有一數值 x出現多於 500 次,最少需要讀取多少個元素才能得取得該數值﹖

A. 1 B. 2 C. 500 D. 1000

25. Below is the truth table for the nor operator:

以下是 nor(或非)的真值表:

A B A nor B

FALSE 假 FALSE 假 TRUE 真 FALSE 假 TRUE 真 FALSE 假

TRUE 真 FALSE 假 FALSE 假 TRUE 真 TRUE 真 FALSE 假

Which of the following expressions is equivalent to ((not A) and B)?

以下哪一表達式等同 ((not A) and B)?

A. A nor (B nor B) B. (A nor A) nor B C. (A nor B) nor B

D. (A nor B) nor (B nor B)

END OF SECTION A

部完

(12)

Section B

部 (20 marks 分)

The blanks are labeled from A to L. Please fill in the blanks on the answer sheet.

下列各空格分別命名為 A 至 L,請在答題紙上對應的地方填上答案。

Note 意:

(1) Select exactly one programming language on the Answer Sheet. Answers must be in that language.

您必須在答題紙上選擇剛好一種編程語言,並只使用該語言作答。

(2) For C and C++, you must not use the?: operator.

對於 C 及 C++,答案不可以包括?: 運算元。

(3) You must not use any library function unless the appropriate library has been included. (See Page 1) 除非適當的函數庫已被引用 (見頁一),否則答案不可以包括任何函數庫內的函數。

(4) You can write only one character in each box on the answer sheet.

答題紙上每個小格只可填上一個字符。

(5) Answers must not exceed the designated number of boxes.

答案長度不得多於該題提供的小格數目。

(6) Write legibly. Unrecognizable answers will be regarded as incorrect.

字體須端正清楚,無法辨別之答案當錯誤論。

(7) If blank X is divided into X1 and X2, it means that marks will only be given when X1 and X2 are both correct.

如果空格 X 分為 X1 和 X2,那麽 X1 和 X2 均為正確才會給分。

1. The input to the following program segment will be 99 distinct integers between 1 and 100 (inclusive).

以下程序段的輸入是 99 個不同的、在 1 和 100 之間(含)的整數。

Pascal

var

i, a, x: longint;

function f(): longint;

begin a := 0;

for i := 1 to 99 do begin

read(x);

a :=     A1    

end;

f :=     A2    

end;

C

int i, a, x;

int f() { a = 0;

for (i = 1; i <= 99; i++) { scanf("%d", &x);

a =     A1    ; }

return     A2    ; }

C++

int i, a, x;

int f() { a = 0;

for (i = 1; i <= 99; i++) { cin >> x;

a =     A1    ; }

return     A2    ; }

Complete functionf()so that it returns the integer between 1 and 100 (inclusive) that does not appear in the input.

完成函數 f()使其回傳在 1 和 100 之間(含)而沒有在輸入出現的整數。

Answer 答案:     A1    

(13)

2. Consider the following program segment: 考慮以下程序段:

Pascal

var

a: array[0..99] of longint;

procedure swap(x, y: longint);

var t: longint;

begin

    B     end;

procedure f(k: longint);

var i: longint = 0;

begin

while (i <     C    ) do begin

swap(i, k - i - 1);

inc(i) end end;

procedure g(k: longint);

begin

    D     end;

C / C++

int a[100];

void swap(int x, int y) { int t;

    B    ; }

void f(int k) { int i = 0;

while (i <     C    ) { swap(i, k - i - 1);

i++;

} }

void g(int k) {     D    ; }

Assume thatais an array of 100 integers andkis an integer between 0 and 100 inclusive.

假設a是一個存有 100 個整數的陣列,而k是一個 0 和 100 之間(含)的整數。

Complete the procedureswapso that it swaps the values ofa[x]anda[y]. You must NOT make use of the proceduresforg.

完成程序swap使得它會交換a[x]a[y]的數值。你不可以利用程序fg Answer 答案:     B     (1 mark 分)

Complete the procedurefso that it reverses the firstkelements in the arraya. 完成程序f使得它會倒轉陣列a的前k個元素。

Answer 答案:     C     (1.5 marks 分)

Complete the proceduregso that it reverses the lastkelements in the arraya. You may make use of the proce- duresswapandf.

完成程序g使得它會倒轉陣列a的後k個元素。你可以利用程序swapf Answer 答案:     D     (1.5 marks 分)

Hint: you can write several program statements in one line by separating them with semi-colon (;).

提示:只要用分號(;)分隔,就可以在一行之內填寫數句程序語句。

(14)

3. Consider a rectangular grid, where the cell labeled withSis the starting-point, cell labeled withTis the target- point, and cells labeled withCare the checkpoints. We call a path an interesting path if and only if it is a path that walk from the starting-point to the target-point by repeatedly walking to either the cell below or on the right, and passing through EXACTLY ONE checkpoint. For example, for the following 3× 3 grid, there are 4 distinct interesting paths:

考慮一個長方網格,標示 S的格子為起點、標示 T的格子為終點、而標示 C的格子為檢查站。我們

稱一條路徑為有趣路徑當且僅當這條路徑是由起點反覆往下面或右面的格子走而到達終點,而且途中

恰好經過一個檢查站。舉例說,對於下面所示的 3× 3 長方網格,一共有 4 條有趣路徑。

S

T C C

Now consider the 5× 5 grid below: (cellA1labeled asSand cellE5labeled asT) 現在考慮下面所示的 5× 5 長方網格:(方格 A1標示為 S、方格 E5標示為 T

S

5 T 4 3 2 1

A B C D E

On the answer sheet, label exactly TWO cells as checkpoints (C) so that the number of distinct interesting paths is EXACTLY 2.

請在答題紙上將恰好兩個方格標示為檢查站 (C),使得有趣路徑的數目恰好是 2。

Answer 答案:     E     (1 mark 分)

On the answer sheet, label exactly TWO cells as checkpoints (C) so that the number of distinct interesting paths is EXACTLY 16.

請在答題紙上將恰好兩個方格標示為檢查站 (C),使得有趣路徑的數目恰好是 16。

Answer 答案:     F     (2 marks 分)

4. Consider a string that consists of 100 lowercase letters. You may remove an arbitrary number of characters in the string without changing the order of the remaining characters. In the worst case, at least how many characters should you remove so that the remaining string is a palindrome? (Hint: Palindromes are strings that read the same forward or reversed. For example, “ABBA” is a palindrome while “ABCD” isn’t.)

考慮一個由 100 個小寫英文字母組成的字串。你可以在不影響剩餘字母次序的情況下,在這個字串中 移除任意數目的字母。在最壞情況,你最少需要移除多少個字母使得剩餘的字串是一個迴文?(提示:

迴文是正讀反讀都一樣的字串。舉例說,“

(15)

5. Complete the functionf(x)so that the functiong(ch)can convert lowercase letters to uppercase and vice versa.

For example,g('A')should returnaandg('q')should returnQ.

完成函數 f(x)使得函數g(ch)可以將小草英文字母轉換成大草英文字母,反過來也一樣。舉例說,

g('A')應傳回a,而g('q')應傳回Q Pascal

function f(x: longint): longint;

begin

f :=     H    

end;

function g(ch: char): char;

var tmp: longint;

begin

tmp := ord(ch);

tmp := f(tmp);

g := chr(tmp) end;

C / C++

int f(int x) {

return     H    ; }

char g(char ch) { int tmp;

tmp = (int)ch;

tmp = f(tmp);

return (char)tmp;

}

Answer 答案:     H     (2 marks 分)

6. Complete the functionfind_sum()so that it returns the sum of all positive numbers innum[0..99]. You may assume that the values stored innum[0..99]are integers between -100 and 100 inclusive.

Special rule: Your answers are NOT allowed to include any of the following characters:<,>and=.

完成函數find_sum()使得它傳回所有在num[0..99]之中正整數的總和。你可假設num[0..99] 的數值都是 -100 和 100 之間(含)的整數。

則:在你的答案中不能包含以下字符:<,>= Pascal

var

num: array[0..99] of longint;

temp, i: longint;

function find_sum(): longint;

begin

temp := 0;

for i := 0 to 99 do

temp := temp +     I1    ; find_sum :=     I2     end;

C / C++

int num[100];

int temp, i;

int find_sum() { temp = 0;

for (i = 0; i <= 99; i++)

temp = temp +     I1    ; return     I2    ;

}

Answer 答案:     I1    

Answer 答案:     I2     (2 marks 分)

(16)

7. Consider the following program segment: 考慮以下程序段:

Pascal

function is_prime(n: longint): longint;

var i: longint;

begin

if (n = 1) then begin

is_prime := 0;

exit end;

    J1     while (i * i <= n) do begin

if (    J2    ) then begin

is_prime := 0;

exit end;

inc(i) end;

is_prime := 1 end;

function prime_count(n: longint): longint;

var answer, i: longint;

begin

answer := 0;

for i := 1 to n do

answer :=     K    ; prime_count := answer

end;

C / C++

int is_prime(int n) { int i;

if (n == 1) { return 0;

}

    J1     while (i * i <= n) {

if (    J2    ) { return 0;

} i++;

}

return 1;

}

int prime_count(int n) { int answer, i;

answer = 0;

for (i = 1; i <= n; i++) answer =     K    ; return answer;

}

It is guaranteed thatnis an integer between 1 and 100 inclusive.

保證n是 1 和 100 之間(含)的整數。

Complete the functionis_prime(n), so thatis_prime(n)returns1ifnis a prime, and0otherwise.

完成函數 is_prime(n),使得 is_prime(n)n是質數時傳回 1,否則傳回 0 Answer 答案:     J1    

Answer 答案:     J2     (1.5 marks 分)

Complete the functionprime_count(n), so thatprime_count(n)returns the number of primes between 1 andn(inclusive).

完成函數 prime_count(n),使得 prime_count(n)傳回 1 和n之間(含)的質數數目。

Answer 答案:     K     (1.5 marks 分)

(17)

8. The input to the following program is 100 integers between 0 and 10000 inclusive. The program tries to output the number of distinct integers in the input. However, the program contains an error that can be fixed by changing exactly one line. Find the line and correct it.

以下程序的輸入為 100 個 0 至 10000 之間(含)的整數。此程序嘗試輸出輸入內有多少個不同的整數,

但是程序中有一錯誤,並只需更改一行便能修正,請找出並將其改正。

Pascal

11 var

12 a: array[0..99] of longint;

13 f: array[0..10000] of longint;

14 i, answer: longint;

15 begin

16 answer := 0;

17 for i := 0 to 10000 do 18 begin

19 f[i] := 0 20 end;

21 for i := 0 to 99 do 22 begin

23 read(a[i]) 24 end;

25 for i := 0 to 99 do 26 begin

27 inc(f[a[i]]) 28 end;

29 for i := 0 to 99 do 30 begin

31 if (f[i] > 0) then 32 begin

33 inc(answer)

34 end

35 end;

36 write(answer) 37 end.

C

41 int a[100];

42 int f[10001];

43 int i, answer;

44 int main() { 45 answer = 0;

46 for (i = 0; i <= 10000; i++)

47 {

48 f[i] = 0;

49 }

50 for (i = 0; i <= 99; i++)

51 {

52 scanf("%d", &a[i]);

53 }

54 for (i = 0; i <= 99; i++)

55 {

56 f[a[i]]++;

57 }

58 for (i = 0; i <= 99; i++)

59 {

60 if (f[i] > 0)

61 {

62 answer++;

63 }

64 }

65 printf("%d", answer);

66 return 0;

67 }

C++

71 int a[100];

72 int f[10001];

73 int i, answer;

74 int main() { 75 answer = 0;

76 for (i = 0; i <= 10000; i++)

77 {

78 f[i] = 0;

79 }

80 for (i = 0; i <= 99; i++)

81 {

82 cin >> a[i];

83 }

84 for (i = 0; i <= 99; i++)

85 {

86 f[a[i]]++;

87 }

88 for (i = 0; i <= 99; i++)

89 {

90 if (f[i] > 0)

91 {

92 answer++;

93 }

94 }

95 cout << answer;

96 return 0;

97 }

Line number 行數:   L1  

Correction 改正:     L2     (2 marks 分)

END OF PAPER

參考文獻

相關文件

If he divided them equally into several groups, each group has the same number of pieces of blue paper and green paper respectively and no paper is

If x or F is a vector, then the condition number is defined in a similar way using norms and it measures the maximum relative change, which is attained for some, but not all

He is best remembered for his plays The Importance of Being Honest and An Ideal Husband and his novel The Picture of Dorian Gray, but he also wrote some

Young people like to play computer

If a contributor is actively seeking an appointment in the aided school sector but has not yet obtained an appointment as a regular teacher in a grant/subsidized school, or he

Playing computer games is interesting for my brother.(To play computer games is interesting for my brother.)(It is interesting for my brother to play computer

Credit to Theory of Computer Games, 2018 Fall.. Normal BFS

Credit to Theory of Computer Games,