• 沒有找到結果。

The Hong Kong Olympiad in Informatics 1997

N/A
N/A
Protected

Academic year: 2021

Share "The Hong Kong Olympiad in Informatics 1997"

Copied!
9
0
0

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

全文

(1)

The Hong Kong Olympiad in Informatics 1997

Heat Event

Time allowed: 1 hour and 30 minutes.

1. This paper consists of ELEVEN questions and carries 247 marks. Different questions may carry different marks.

2. Write ALL your answers in the answer book provided. Any answer written on this question book will not be marked.

3. Use either the QuickBASIC or the Pascal programming language. Use ONLY ONE programming langauge throughout the paper. Indicate your choice of programming language in the space provided in the answer book.

Organisers:

Education Department, Hong Kong

The Hong Kong Association for Computer Education The Hong Kong Computer Society

(2)

1. Write down the output of the following program segments:

(QuickBASIC version) (Pascal version)

(a) A = 5 A := 5;

B = 4 B := 4;

PRINT A; B writeln (A, ' ', B);

C = A C := A;

A = B A := B;

B = C B := C;

PRINT A; B; C writeln (A, ' ', B, ' ', C);

(b) K = 0 K := 0;

DO WHILE K <= 5 while K <= 5 do begin

K = K + 2 K := K + 2;

PRINT K; write(K, ' ')

LOOP end;

(c) X = 1 X := 1;

Y = 2 Y := 2;

Z = 3 Z := 3;

IF X > Y THEN if X > Y then

PRINT X writeln(X)

ELSE else

IF Y < Z THEN if Y < Z then

PRINT Y writeln(Y)

ELSE PRINT Z else writeln(Z);

END IF END IF

(10 marks) 2. The program segment below is used to evaluate the following expression:

1 1

1 1 2

1 1 2 3

1 1 2 3 4

1 1 2 3 4 5

 

  

    Complete the program segment by filling the blanks.

(QuickBASIC version) (Pascal version)

S = ??? S := ??? ;

T = ??? T := ??? ;

FOR K = 2 to 5 for K := 2 to 5 do

T = ??? begin

S = ??? T := ??? ;

NEXT K S := ???

PRINT USING "#.######";S end;

writeln (S:8:6);

(3)

procedure called Compare is used to compare two numbers. Compare (P,Q) compares two numbers P and Q and returns the result (a) P=Q or (b) P>Q or (c) P<Q.

Fill in the blanks so that the larger number can be found by using the procedure Compare only 4 times as described below.

Step 1 Let S1 = A1 + A2 + A3 + . . . + A27 S2 = A28 + A29 + A30 + . . . + A54 Compare (S1,S2):

if S1 > S2 take k = 0 S1 < S2 take k = 27 S1 = S2 take k = 54

Step 2 Let S1 = Ak+1 + Ak+2 + Ak+3 + . . . + Ak+9 S2 = Ak+10 + Ak+11 + Ak+12 + . . . + Ak+18 Compare (S1,S2):

if S1 > S2 take k = ???

S1 < S2 take k = ???

S1 = S2 take k = ???

Step 3 Let S1 = Ak+1 + Ak+2 + Ak+3 S2 = Ak+4 + Ak+5 + Ak+6 Compare (S1,S2):

if S1 > S2 take k = ???

S1 < S2 take k = ???

S1 = S2 take k = ???

Step 4 Let S1 = Ak+1 S2 = Ak+2 Compare (S1,S2):

if S1 > S2 take k = ???

S1 < S2 take k = ???

S1 = S2 take k = ???

Step 5 Output: "The larger number is"; Ak+1

(9 marks)

(4)

4. Given the following program segment.

(QuickBASIC version) (Pascal version)

IF (A > B) AND (C < 0) THEN if (A > B) and (C < 0) then IF B > 5 THEN if B > 5 then

PRINT "X" writeln('X')

ELSE else

PRINT "Y" writeln('Y')

END IF else

ELSE writeln('Z');

PRINT "Z"

END IF

It is found that at least three sets of test data are required to test the different logical paths in the program segment. Suggest three sets of test data and the expected output.

(12 marks)

5. Study the following program segment and write the output after the execution of the program segment.

(QuickBASIC version) (Pascal version)

PROCESS(3) procedure PROCESS (A : integer);

END begin

if A > 10 then

SUB PROCESS (A) writeln('HERE')

IF A > 10 THEN else begin

PRINT "HERE" writeln(A);

ELSE PROCESS(A + 2)

PRINT A end

PROCESS (A + 2) end;

END IF

END SUB begin

PROCESS(3) end.

(10 marks)

(5)

6. The following program segment was written by Siu Ming:

(QuickBASIC version)

PRINT " 10 9 8 7 6 5 4 3 2 1 "

PRINT " 10 9 8 7 6 5 4 3 2 "

PRINT " 10 9 8 7 6 5 4 3 "

PRINT " 10 9 8 7 6 5 4 "

PRINT " 10 9 8 7 6 5 "

PRINT " 10 9 8 7 6 "

PRINT " 10 9 8 7 "

PRINT " 10 9 8 "

PRINT " 10 9 "

PRINT " 10 "

(Pascal version)

writeln (' 10 9 8 7 6 5 4 3 2 1 ');

writeln (' 10 9 8 7 6 5 4 3 2 ');

writeln (' 10 9 8 7 6 5 4 3 ');

writeln (' 10 9 8 7 6 5 4 ');

writeln (' 10 9 8 7 6 5 ');

writeln (' 10 9 8 7 6 ');

writeln (' 10 9 8 7 ');

writeln (' 10 9 8 ');

writeln (' 10 9 ');

writeln (' 10 ');

You are required to write a program segment using no more than two output statements to produce the same output as Siu Ming’s program segment.

(10 marks)

7. A merchant wants to buy some diamonds from a company. The cost of a diamond is $1234 per carat if the weight of the diamond is less than 3 carats, and the cost is

$2345 per carat if the weight of the diamond is 3 carats or more. Write a program to find the total cost of the diamonds that the merchant buys. The weight of each diamond is entered through the keyboard during program execution.

(Note: You may consider to include input prompt and any necessary consideration in your program.)

(20 marks)

(6)

8. Inside an equilateral triangle of side S, one equilateral triangle of side S/2 can be drawn. The newly drawn equilateral triangle is shaded as shown in Figure 8-1.

Inside each of the 3 unshaded equilateral triangles of side S/2, an equilateral triangle of side S/4 can be drawn. The 3 newly drawn equilateral triangles are shaded as shown in Figure 8-2. Similarly, inside each of the 9 unshaded equilateral triangles of side S/4, an equilateral triangle of side S/8 can be drawn. The 9 newly drawn equilateral triangles are shaded as shown in Figure 8-3.

Write a program segment to find the minimum number of shaded triangles such that the total area of all shaded triangles is greater than 90% of the area of the original equilateral triangle of side S.

Figure 8-1 Figure 8-2 Figure 8-3

(30 marks)

9. 4-digit numbers can be formed by using four of the following eight digits 1,2,3,4,5,6,7 and 8.

(a) Suppose each of the eight given digits can be used more than once, write a program to generate ALL possible distinct 4-digit numbers. For example, 1111, 1234, 2134, 6236, and 8154 are some of the 4-digit numbers.

(b) Suppose each of the eight given digits can be used only once, write a program to generate ALL possible distinct 4-digit numbers. For example, 1234, 2134, and 8154 are some of the 4-digit numbers, but 1111 or 6236 are not.

(40 marks)

(7)

10. Linear Search and Binary Search are two searching algorithms. Their algorithms are shown below where Num represents the name of a data array, Num[i] represents the ith element in the data array Num, N stores the number of elements in the array, and Target stores the target number to be searched.

Linear Search

Set P to 0 and CNT to 0 Repeat

Increase the value of CNT by 1 --- (*) Until Num[CNT] = Target or CNT > N

If CNT is not larger than N, set P to CNT

Binary Search (The array Num should have been sorted in ascending order.) Set Head = 1, Tail = N

Repeat

Set P to the integer quotient when the sum of the values in Head and Tail is divided by 2

if Num[P] > Target then set Tail to P - 1

if Num[P] < Target then set Head to P + 1 --- (**) Until Num[P] = Target or Head > Tail

If Num[P] is not equal to Target then set P to 0

Note : In both algorithms, if the value stored in Target is found in Num, P stores the position of the number in Num which matches the value of Target. Otherwise, the value of P is assigned to be 0.

(a) Suppose the value of Target is 37 and the array Num stores the following seven numbers 1, 23, 26, 45, 58, 93, and 102.

(i) Write down the content of Num[CNT] each time the statement (*) is executed when the Linear Search algorithm is used.

(ii) Write down the content of Num[P] each time the statement (**) is executed when the Binary Search algorithm is used.

(b) If there are 2 000 000 numbers stored in the array Num, find the maximum number of times the statement

(i) (*) is executed when the Linear Search algorithm is used; and (ii) (**) is executed when the Binary Search algorithm is used.

(c) Using recursive subprogram, write a program segment to implement the Binary Search algorithm.

(40 marks)

(8)

11. When searching articles in a library by using keywords, exact match of the keywords in an article is not required. If the keywords in an article are separated by other words, or appeared in reverse order, the search is still considered successful.

For example, if the keywords are "Computer" and "Education", then articles containing the words "computer education" or "computer in education" or

"education by using computer" are considered satisfying the searching criteria.

However, this approach will find too many articles which may not be useful to the user. Hence, it is desirable to calculate a relevance score for each article. Such a score tells the user roughly how relevant the article is related to the keywords supplied by the user. A relevance score, S, for searching based on a pair of distinct keywords (K1 and K2) is defined as S = ( A * B ) / ( C + 1), where

S is the relevance score;

A is the number of occurrence of the first keyword (K1) in the article;

B is the number of occurrence of the second keyword (K2) in the article;

C is the shortest distance between any occurrence of K1 and any occurrence of K2 in the article.

The values of A, B and C are determined according to the following definitions:

A word is defined as an alphanumeric string delimited by non-alphanumeric character(s) such as blank(s), punctuation mark(s), etc.

An occurrence of a keyword in an article is defined as that there is a word with exactly the same spelling (case insensitive) with the keyword.

The distance between a pair of keywords in an article is defined to be the number of words between them. The minimal of all possible values of distance is the shortest distance between any occurrence of the two keywords in the article. If no pair of keywords can be found in the article, the shortest distance is defined to be zero. An example is given below.

Keywords Computer Education

Article ART1 ...computer assisted learning in education....education by computer....

...Computer application in education Article ART2 ...education in computer education Article ART3 ...education in society....

Then, the values of S, A, B, and C for the articles ART1, ART2 and ART2 are:

Vaule of A Vaule of B Vaule of C Vaule of S

Article ART1 3 3 1 4.5

Article ART2 1 2 0 2

Article ART3 0 1 0 0

(9)

Write a program to accept two keywords from the keyboard, read an article from a text file, compute the relevance score of the article with respect to the keywords, and then display the relevance score on screen.

The article is stored in a text file named "INPUT.TXT". The first line in the file is an integer indicating the number of text lines that follows. The length of each text line is less than 256 characters and the total number of words in the article is less than 2000.

Sample Run

Enter two keywords? Computer Education Relevance score = 34.5

(60 marks) End of Paper

參考文獻

相關文件

You are given the wavelength and total energy of a light pulse and asked to find the number of photons it

(1) principle of legality - everything must be done according to law (2) separation of powers - disputes as to legality of law (made by legislature) and government acts (by

好了既然 Z[x] 中的 ideal 不一定是 principle ideal 那麼我們就不能學 Proposition 7.2.11 的方法得到 Z[x] 中的 irreducible element 就是 prime element 了..

volume suppressed mass: (TeV) 2 /M P ∼ 10 −4 eV → mm range can be experimentally tested for any number of extra dimensions - Light U(1) gauge bosons: no derivative couplings. =&gt;

For pedagogical purposes, let us start consideration from a simple one-dimensional (1D) system, where electrons are confined to a chain parallel to the x axis. As it is well known

The observed small neutrino masses strongly suggest the presence of super heavy Majorana neutrinos N. Out-of-thermal equilibrium processes may be easily realized around the

incapable to extract any quantities from QCD, nor to tackle the most interesting physics, namely, the spontaneously chiral symmetry breaking and the color confinement.. 

(1) Determine a hypersurface on which matching condition is given.. (2) Determine a