All Ireland Schools Programming Competition
Day 2 Problems
There are three problems contained in this document. Attempt as many as you can.
Important
For each problem you must save your program as ‘dayNP.ext’ where N is the competition day (1 or 2), P is the particular problem (A, B, or C), and ext is the extension (BAS, BC, C, CPP or PAS). For example if you write a Pascal program to solve problem A on day 2 the name should be day2a.pas. Your program should read its input from a file called day2a.dat and write the solution to a file called day2a.sol.
With each problem are two examples. These examples will be used (in addition to other unseen tests) to test your program.
Note that if your program generates correct results for these test files, it does not mean that your program will give correct output for all possible input.
There is a batch file test.bat on the D: drive that will mark your program. If you type ‘test 2 a 1’, it will test day2a.exe or day2a.bas with example 1. So when you have written day2a, you can test it using the two example files that you are given using the commands
test 2 a 1
and
test 2 a 2
You can similarly test your programs, day2b and day2c.
When you are finished, ensure that you have saved your files onto the root directory of the I: drive. (That is, a BASIC program for problem A should be saved as I:\day2a.bas.) You will also be given a floppy disk at the end of the session, and you must also save your files to this disk and give it to a supervisor.
Best of luck
Note to BASIC users: if you want your program to be compiled you should save it as ‘day1a.bc’, otherwise you should save it as ‘day1a.bas’. To compile your program type ‘cbas day1a’ note that cbas day1a.bc won’t work.
Note that I:\ is equivalent to C:\test.
Problem 2A
Capitals to the fore
Description
You are given a string of letters. You must split these letters into capitals and non-capitals.
Input
There will be a line containing the string, s, which will have at most 12 letters. There will be at least one capital letter and at least one non-capital.
Output
The first line contains the capital letters of s. The second line contains all remaining letters of s. The letters in each group must appear in the same order as they appeared in the initial string.
Example 1
Input
coMpetItION
Output
MIION
copett
Example 2
Input
ProGraMMing
Output
PGMM
roraing
Problem 2B
Random Numbers
Description
Computer programs (e.g. games) frequently require random numbers. One way to create random* numbers is to use the formula
xn = (xn-1 + step) MOD N
step and N are two constant values, xn is the next random number, xn-1 is the previous random number and MOD is the remainder operation (divide the number and keep the remainder). An example should make this clear. Suppose step is 3 and N is 5, the formula yields
0 All sequences start at 0
(0+3) MOD 5 = 3 3 divided by 5 leaves a remainder of 3.
(3+3) MOD 5 = 1 6 divided by 5 leaves a remainder of 1.
(1+3) MOD 5 = 4
(4+3) MOD 5 = 2
(2+3) MOD 5 = 0 Back where we started
(0+3) MOD 5 = 3
This gives a sequence 0, 3, 1, 4, 2 and back to 0 again.
The sequence will always repeat at some point. You must write a program to determine when a particular sequence will repeat and what is the last number before repetition occurs (in this case 2).
Input
The input will contain two integers, step and N, (1 £ step £ N £ 1000).
Output
First of all the number indicating the length of the sequence (i.e. before it repeats). On the next line should be the last number generated before repetition occurs.
Example 1
Input
3 5
Output
5
2
Example 2
Input
15 20
Output
4
5
Problem 2C
City heights
Description
Imagine a city consisting only of buildings shaped like rectangular blocks. You will be given the positions and heights of these buildings and must write a program to determine which buildings are visible from a south view.
Luckily for you, this is a fairly boring city and not only are the buildings rectangular blocks, but their sides are parallel to the compass points (i.e. they go north-south or east-west).
See for example the map (below left). The buildings are labelled on the top left hand side and their heights are shown in the lower right. There are 14 buildings, and looking from the south (i.e. from the bottom of the page) you would see the view below.

Note that the visible buildings are shown shaded in the map.
Input
The first line contains an integer N, (1 £ N £ 100), the number of buildings. Following that will be N lines representing the buildings. Each building is represented by five positive integers separated by spaces. The first two numbers correspond to the x and y coordinates of the building. (The south-west corner is 0, 0.) The next three numbers give the width (length of the south side), depth (length of the west side) and height respectively.
Output
The buildings are numbered 1 to N corresponding to their position in the input file. The output will contain the numbers representing the buildings that are visible from the south. They should be ordered from the west to the east (left to right). If two buildings have the same x coordinate, then the most southerly (i.e. nearest the viewer) should come before the other.
There should be no more than ten numbers in a line. Every tenth number you should create a new line, otherwise use a space to separate the numbers.
Example 1
Input
14
160 0 30 60 30
125 0 32 28 60
95 0 27 28 40
70 35 19 55 90
0 0 60 35 80
0 40 29 20 60
35 40 25 45 80
0 67 25 20 50
0 92 90 20 80
95 38 55 12 50
95 60 60 13 30
95 80 45 25 50
165 65 15 15 25
165 85 10 15 35
Output
5 9 4 3 10 2 1 14
Example 2
Input
3
0 8 2 2 1
0 1 1 1 3
1 0 2 1 2
Output
2 3
Example 3
Input
21
0 0 1 1 1
2 0 1 1 1
4 0 1 1 1
6 0 1 1 1
8 0 1 1 1
10 0 1 1 1
12 0 1 1 1
14 0 1 1 1
16 0 1 1 1
18 0 1 1 1
20 0 1 1 1
22 0 1 1 1
24 0 1 1 1
26 0 1 1 1
28 0 1 1 1
30 0 1 1 1
32 0 1 1 1
34 0 1 1 1
36 0 1 1 138 0 1 1 1
40 0 1 1 1
Output
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21
Instructions for the Extra Problem
This problem will only be considered to distinguish between contestants whose scores are otherwise equal. You should ensure that you’ve got as many marks as possible in the other problems before attempting this one.
None of the test data will be seen, and there are no marks for producing an output file.
Otherwise this problem is the same as the others; you must save your program as ‘day2d.ext’ where ext is the extension (PAS, C, CPP or BC, BAS). Your program should read its input from a file called day2d.dat and write the solution to a file called day2d.sol.
Problem 2D
Overflowing Barrels
Description
There are a number of 100 litre barrels standing upright on a horizontal surface. Every barrel is connected to every other barrel by a pipe (connected to the bottom of the barrel). Each pipe has a valve that can be open or closed. If two barrels are connected by an open pipe then the liquid moves instantly between the barrels until the amount liquid in each barrel is equal. If the valve is closed, no liquid will flow between the barrels.
Initially all barrels are empty and all valves are closed.
There are two possible operations
1. Pour some liquid into a barrel. This is indicated by the command "P n m", where n indicates the barrel and m is the amount of liquid in litres. m is an integer (1 £ m £ 1000).
2. Change the state of a valve (from open to closed or closed to open). This is indicated by the command "V n1 n2", where n1 and n2 are two different barrels. Note that t "V n1 n2" is equivalent to "V n2 n1".
Your goal is to execute a given sequence of operations and if one of the barrels overflows, say what operation caused the overflow. Otherwise state the largest and smallest volume of liquid that occurred in any of the barrels at the end of the sequence of operations.
Neglect the amount of liquid in the pipes.
Input
The input will contain two integers, N (1 £ N £ 100), the number of barrels and K (1 £ k £ 1000), the number of operations. The following K lines contain one operation each.
Output
If no overflow occurs, then your program must print "OK" followed by the minimum and maximum volumes that occur in the barrels. Otherwise you must print "OVERFLOW" followed by the number of the operation during which the overflow occurred.
Example 1
Input
2 6
P 1 63
P 2 37
V 1 2
P 1 50
V 2 1
P 1 20
Output
Ok 75.00 95.00
Example 2
Input
3 8
P 1 100
V 2 1
P 2 40
V 1 2
V 1 3
V 1 3
P 3 70
V 1 3
Output
OVERFLOW 7