Westminster College 2005 High School Programming Contest October 18, 2005

advertisement
Westminster College
2005 High School Programming Contest
October 18, 2005
Rules:
1. There are six questions to be completed in two and 1/2 hours.
2. All questions require you to read the test data from standard input and write results to standard
output. You should not use les for input or output.
3. The allowed programming languages are C++ and Java.
4. All programs will be re-compiled prior to testing with the judges' data.
5. Programming style is not considered in this contest. You are free to code in whatever style you
prefer. Documentation is not required.
6. Judges' decisions are to be considered nal. No cheating will be tolerated.
2005 Westminster High School Programming Contest
Problem A:
1
Cummings and Goings
Edward Estlin Cummings was an American poet who lived between 1894 and 1962. While linked early
in his career with the Modernist movement, he wrote poems with themes that more closely resemble
the works of the New England Transcendentalists and English Romantics. But for the purpose of
this problem what most interests us about e.e. cummings was his eccentric use of punctuation and
capitalization. Many of his poems are devoid of both, and that is what you will do for this problem.
Input
There will be multiple input sets. Each test case will start with an integer n indicating the number
of lines of input for that case. Following that will be n lines of text, each containing no more than 80
characters. A value of n = 0 indicates end of input and should not be processed.
Output
For each test case, rst output the line Case n:, where n is the case number starting at 1. Then output
the text for that case exactly as it was input except for the following two changes: 1) all uppercase
letters should be changed to lowercase, and 2) all punctuation should be removed. For the purposes of
this problem, the punctuation characters are any characters which are not in the following: space, tab,
newline, `A'...`Z', `a'...`z', `0'..`9', `-', `(', `)' and single and double quotation marks ' and ". Follow each
test case with a blank line.
Sample Input
1
ABC, "ABC", "@BC", <ABC>
2
Here's an example - spanning 2 lines - showing which characters to keep, and which to remove(!). Any questions?
4
My mind is
a big hunk of irrevocable nothing, which touch and
taste and smell and hearing and sight keep hitting and
chipping with sharp fatal tools.
0
Sample Output
Case 1:
abc "abc" "bc" abc
Case 2:
here's an example - spanning 2 lines - showing which characters to keep and which to remove() any questions
Case 3:
my mind is
a big hunk of irrevocable nothing which touch and
taste and smell and hearing and sight keep hitting and
chipping with sharp fatal tools
2
2005 Westminster High School Programming Contest
Problem B:
All Hands on Derek
Derek loves to play Battleship, and wins almost every time for one very important reason: he cheats. If
you will recall, Battleship is played on a 10 by 10 grid, with rows labeled by letters A to J, and columns
labeled by integers 0 to 9 (I know, I know its really 1 to 10, but 0 to 9 make things work out easier
for us). Each player places 5 ships on the grid. The ship sizes are 5, 4, 3, 3 and 2 grid squares, and
they must be placed in either a row or a column (not diagonally). The grid below shows one possible
arrangement of the ships:
0
A
B
C
D
E
F
G
H
I
J
1
2
3
4
5
6
7
8
9
The object of the game is to sink the other persons ships by \shooting" at various grid locations. After
announcing the grid square you are attacking, your opponent tells you whether it was a \hit" (i.e. one
of his ships was on that square) or a \miss". If Derek's opponent were to attack grid square C6, an
honest player would announce that one of his ships had been hit. But not Derek. He keeps track of all
of the shots of his opponent, and constantly picks up and moves his ships to locations which have not
hit yet. In this case, he could move the ship one row higher or lower, but not two rows lower since that
would bump into another ship (though he could then move that ship) and not one column over to the
right, since his boat will still lie on square C6. If his opponent had previously shot at square C1, then
Derek could not move his ship one column over to the left either. For this problem you will be given
a set of grid squares where shots have been taken, and you need to determine if it's still possible for
Derek to place all his ships so that no ship is hit yet.
(over)
3
2005 Westminster High School Programming Contest
Input
There will be multiple input sets. Each input set will consist of a single line of the form
n
n g1 g2 g3 : : : g
where n is the number of grid squares which have been shot, and g1 ; : : : ; gn are the square locations.
Each location will consist of two characters: an uppercase letter indicating the row followed by a digit
indicating the column. A line which begins with 0 indicates end of input and should not be processed.
Output
For each input set, output either the word \Yes" or \No" depending on whether or not Derek can still
place his ships without any being hit or not.
Sample Input
1 C6
20 A4 B3 C2 D1 E0 A9 B8 C7 D6 E5 F4 G3 H2 I1 J0 F9 G8 H7 I6 J5
0
Sample Output
Yes
No
2005 Westminster High School Programming Contest
Problem C:
4
A Rational Explanation
The editor of a local newspaper is concerned about the presentation of mathematical information in
his stories. He doesn't want his readers to be overly confused by what they read { he wants to keep
the mathematical explanations as simple as possible. For example, instead of quoting the statistic \97
out of every 108 people believe computer programmers are sexy", he would rather simplify it to \9
out of every 10 people believe computer programmers are sexy". He would like some sexy computer
programmer to write a general program to simplify ratios such as this.
Input
There will be multiple test cases. The rst line will contain a single integer n indicating the number of
test cases. Each test case will be a single line of the form x out of y, where x and y are two positive
integers. All values of x and y will be less than one million.
Output
For each test case, output a line of the form n out of m, where n and m are two non-negative integers
10 such that the fraction n/m is as close to x/y as possible. If there is a tie between two dierent sets
of n and m, pick the one with the lowest value of n.
Sample Input
3
97 out of 108
5 out of 10
20690 out of 48273
Sample Output
9 out of 10
1 out of 2
3 out of 7
2005 Westminster High School Programming Contest
Problem D:
5
Subtraction Problem
Assume we have 4 non-negative integers (a; b; c; d). Suppose we now replace each value with the absolute
value of dierence between that value and the value which follows it, wrapping around when we get to
d. In other words, we replace a with ja
bj, b with jb
cj, c with jc
dj and d with jd
aj. For example,
if we use today's date to get our values and apply the above transformation we get the following
(10; 18; 20; 5) ! (8; 2; 15; 5)
We can repeat this transformation multiple times. The rst ve applications in the example above
would be
(10; 18; 20; 5) ! (8; 2; 15; 5) ! (6; 13; 10; 3) ! (7; 3; 7; 3) ! (4; 4; 4; 4) ! (0; 0; 0; 0)
It can be shown that no matter what four integers you start with, you will eventually get to (0; 0; 0; 0).
The object of this problem is to determine exactly how long that will take.
Input
There will be multiple test cases. The rst line of the input will be an integer n indicating the number
of test cases. Each test case will consist of a single line containing the four integers a; b; c and d.
Output
For each test case, output the number of transformations needed to get to (0; 0; 0; 0).
Sample Input
3
10 18 20 5
10 18 2005 0
1 2 3 5
Sample Output
5
5
7
2005 Westminster High School Programming Contest
Problem E:
6
It's in the Cards
Here's a simple game to play with elementary school kids to teach them basic math skills. Get a deck
of cards (removing the face cards) and lay one face up on a table. Then deal each player one card
which only he/she sees. Each player announces the sum of their card and the face up card. After
that, the player who rst announces the sum of all of the player's cards wins. For example, suppose
a 5 is dealt face up on the table, and three players { Amy, Bob and Cal { each receive a 6, 2 and 10,
respectively. Amy announces 11, Bob announces 7 and Cal announces 15. After a few seconds, Amy
wins by announcing 18, which is the sum of the three cards dealt to the players. You will write a simple
program to play this game, always assuming that three people are playing.
Input
There will be multiple test cases. Each case will consist of one line containing either 1 or 4 values. The
rst value f will be the face-up card value. If f = 0 then you have reached the end-of-input and should
exit your program. If f > 0, then there will be three other integers on the line indicating the announced
values for each of the players.
Output
For each test case, output the sum of the three players' cards.
Sample Input
5 11 7 15
2 12 12 12
0
Sample Output
18
30
7
2005 Westminster High School Programming Contest
Problem F:
Image is Everything
For this problem, we will work on a simple image processor. The processor will respond to the following
sets of commands:
Rotate Right - rotate the image 90 clockwise
Rotate Left - rotate the image 90 counter-clockwise
Flip Horizontal - ip the image along the horizontal
Flip Vertical - ip the image along the vertical
Double - increase the size of the image by a factor of 2
The gure below give an example of what each of the operations will do. To simplify the input, there
will be no spaces in the image.
PPP.
Q..Q
RRR.
S...
T...
TSRQP
..R.P
..R.P
...Q.
.Q...
P.R..
P.R..
PQRST
T...
S...
RRR.
Q..Q
PPP.
Original
Rotate Right
Rotate Left
.PPP
Q..Q
.RRR
...S
...T
Flip Horiz. Flip Vert.
PPPPPP..
PPPPPP..
QQ....QQ
QQ....QQ
RRRRRR..
RRRRRR..
SS......
SS......
TT......
TT......
Double
Note that for the Double command every character is replaced with a 2 2 block of the same character.
Your program will read in an initial image, apply one or more of these operations to the image and then
output the result
Input
There will be multiple test cases. Each test case will start with a line of the form
n m s
where n and m are the number of rows and columns in the original image, and s is a non-empty string
of letters indicating which operations to perform on the image. A character r indicates rotate right, l
rotate left, h ip horizontal, v ip vertical and d double. The operations should be performed in the
order they appear in the string. Thus if the string was ldh you would rst rotate the image left, then
double it, and the ip it along the horizontal. The values of n; m will be 10, the length of s will be
10, and there will be at most two double commands in any string.
Following this rst line will be n lines with m characters each which species the image (the end-of-line
character does not count as one of the m characters). Values of n = 0 and m = 0 indicate end-of-input
and should not be processed (there will be no string s on this line).
Output
For each test case, output the resulting image. Print a blank line after each case.
2005 Westminster High School Programming Contest
Sample Input
5 4 l
PPP.
P..P
PPP.
P...
P...
2 2 ldh
@b
cd
0 0
Sample Output
.P...
P.P..
P.P..
PPPPP
@@cc
@@cc
bbdd
bbdd
8
Download