The Hong Kong Olympiad in Informatics 1997
Final Event (A.M. Session)
10 March 1997 (Monday) Time allowed: Two hours
Organisers:
Education Department, Hong Kong
The Hong Kong Association for Computer Education The Hong Kong Computer Society
1. Diamond Pattern (20 marks)
Program Name: PROGRAM1.EXE
Input File: SAMPLE1.DAT
We can use characters instead of lines to draw a diamond pattern. The following figure is an example of using the character ‘X’ to draw a diamond pattern with length of each side equal to three X s.
X X X X X X X X
You are asked to write a program to draw a number of diamond patterns using a number of characters. The character(s) used to form the diamond patterns is/are stored in the input file SAMPLE1.DAT. Your program should accept a filename from the keyboard, and read the input data from this file. Each character in the input data will form one diamond pattern.
The first character will form the innermost diamond pattern with three characters on each side. Subsequent character(s), if any, will form the outer diamond pattern(s) with side larger than the immediate preceding diamond pattern by 2 characters.
Input Data
In the input text file SAMPLE1.DAT, there is only one line of text containing N (1 £ N £ 5) characters. You can assume that the input data are any printable characters.
Example Input and Output SAMPLE1.DAT
AC
Below is a sample output of the program. In these outputs, all the information following the question mark is entered through the keyboard by the user. All other items are output from the program.
Data file? SAMPLE1.DAT C
C C C A C C A A C C A A C C A A C C A C C C C
2. Photocopying (20 marks)
Program Name: PROGRAM2.EXE
Input File: SAMPLE2.DAT
In a certain school, there is one photocopying machine for use by students. To minimise money changing hands, each student is required to pay for the photocopying service monthly. At the photocopying machine, after each use, the student’s name and the number of photocopies made are recorded and appended to a data file.
At the end of each month, the data are processed to calculate the fee payable by each student who has made use of the service. The fee for each copy is 20 cents.
You are asked to write a program for the students’ association to calculate the fee charged to each student. Your program should accept a filename from the keyboard, and read the input data from this file. Your output should include the students’ names and the amount payable by them displayed as shown in the sample output below. You should also note that the students’ names are printed in ascending order.
Input Data
In the input text file SAMPLE2.DAT, the first line contains the number of records, N (where 1 £ N £ 200). Each of the following N lines represents a record of the photocopying machine. In each line, the first 15 characters represent a student’s name in capital letters.
The next two characters represent the number of copies made. You should note that there is a space before a 1-digit number.
Example Input and Output
SAMPLE2.DAT 4
CHAN A B 20 MA X Y 13 CHAN A B 38 HO C D 7
Below is a sample output of the program. In these outputs, all the information following the question mark is entered through the keyboard by the user. All other items are output from the program.
Data file? SAMPLE2.DAT STUDENT AMOUNT --- CHAN A B 11.60 HO C D 1.40 MA X Y 2.60
3. Approximation (40 Marks)
Program Name: PROGRAM3.EXE
Input File: SAMPLE3.DAT
Write a program to display the approximated value for a real number N correct to S significant figures. Your program should accept a filename from the keyboard, and read the input data (i.e. N and S) from this file. Output the approximated value to the screen in the format as shown in the sample output below.
Input Data
In the input text file SAMPLE3.DAT, there are two lines of text. The first line contains a number N which is the real number to be approximated. You may assume that the number is in any valid real number format except the scientific notation. The number of digits for N is at most 30. The second line contains an integer S (1 £ S £ 30) which represents the number of significant figures of the approximation.
Example Input and Output
SAMPLE3.DAT 1234567 4
Below is a sample output of the program. In these outputs, all the information following the question mark is entered through the keyboard by the user. All other items are output from the program.
Data file? SAMPLE3.DAT
Approximated value = 1235000
4. Path Product (60 Marks)
Program Name: PROGRAM4.EXE
Input File: SAMPLE4.DAT
A set of N non-negative integers are arranged in a rectangular form of R rows and C columns (where N=R x C). The figure below shows an example of 6 integers arranged in 2 rows by 3 columns.
1 3 5
2 4 6
We can draw a path from the top left corner (i.e. row 1, column 1), passing through some numbers and ending at the bottom right corner (row R, column C). A path is considered valid only if it starts from the top left corner, extends either to the right or downward, and ends at the bottom right corner. Other paths are considered invalid. The “path product” of a valid path is calculated by multiplying all the integers along the path.
You are asked to write a program to find (i) the smallest path product; and (ii) the greatest path product of the integers. Your program should accept a filename from the keyboard, and read the input data from this file. Output the answers to the screen in the format as shown in the sample output below.
Input File
In the input text file SAMPLE4.DAT, there are R+1 lines of text (where 0 < R < 20). The first line contains two integers R and C (where 0 < C < 20) which are separated by a space.
Each of the following R lines contains C non-negative integers. All these non-negative integers are smaller or equal to 10 and each is separated by a space from one another.
These R lines of integers represent the R x C rectangle of non-negative integers.
Example Input and Output
SAMPLE4.DAT 2 3
1 3 5 2 4 6
Below is a sample output of the program. In these outputs, all the information following the question mark is entered through the keyboard by the user. All other items are output from the program.
Data file? SAMPLE4.DAT Smallest = 48
Greatest = 90
5. Treasure Hunting (60 Marks)
Program Name: PROGRAM5.EXE
Input File: SAMPLE5.DAT
Some treasures of gold coins and jewels are buried in an underground cave. Someone has identified the positions of the treasures and has drawn a treasure map. The map is rectangular in shape and is divided into a number of cells. There are R rows and C columns in the map. Each cell is represented by a number pair (r,c). For example, the cell at the top left corner is represented by (1,1). Each cell may represent:
a wall
a free cell (free to go in or out)
the entrance
a cell occupied by a monster
the treasure
The exit of the cave is any free cell on the four sides of the map. As there is a shortage of fresh air supply, you are asked to write a program to find out the shortest path of the excursion that starts at the entrance, passes through ALL the positions of the treasures, and reaches one of the exits of the cave. The path should not pass through any wall or a cell occupied by a monster.
Display on the screen (i) the length of the path, i.e. the number of cells gone through by the path; (ii) the position of each cell along the path. If there are more than one shortest path, display only one of them. If there is no such path, display “No solution”. Your program should accept a filename from the keyboard, and read the input data from this file. Output the answers to the screen in the format as shown in the sample output below.
Input File
In the input text file SAMPLE5.DAT, there are R+1 lines of text (where 1 < R < 11). The first line contains two integers R and C (where 1 < C < 11) which are separated by a space.
Each of the following R lines contains C symbols. The R lines of symbols represent the treasure map of R rows by C columns.
The symbols used in the map include 1: wall
0: free cell
S: entrance to the cave (There is only one entrance to enter the cave.) M: cell occupied by a monster
T: position of the treasure (There are at most 2 “T”s in the map.)
Example Input and Output
SAMPLE5.DAT 6 10
1101001110 1101011010 11S0001001 11M1011101 10T0000001 1101111100
Below is a sample output of the program. In these outputs, all the information following the question mark is entered through the keyboard by the user. All other items are output from the program.
Data file? SAMPLE5.DAT
The length of the shortest path is 8.
The path is :
(3,3),(3,4),(3,5),(4,5),(5,5),(5,4),(5,3),(6,3)exit