Download Chapter 1: Introduction

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Addition wikipedia , lookup

Elementary mathematics wikipedia , lookup

Transcript
Spring Programming Competition
February 23, 2008
A Contest Between Friends
1.
Are You My Friend?
(friend1)
2.
A Sign of the Times?
(sign)
3.
Auto Selection
(auto)
4.
Bigger Better Bowling
(bowling)
5.
Clocks Going Cuckoo
(clocks)
6.
Dice Going Clockwise
(dice)
7.
Desired Drug Dosing
(drug)
8.
Divisible Dot Product
(product)
9.
ISBN Conversion
(isbn)
10. Who is My Friend?
(friend2)
The name in parenthesis following each problem is the
name you must use as your program’s name. You must add
the appropriate extension depending upon your choice of
programming language (.c .cpp .java .py).
Are You My Friend?
February is the month of love and friendship. Numbers can be friends too. It is
said that when Pythagoras was asked "What is a friend?", he replied that a friend
is one "who is the other I such as are 220 and 284."
One definition of friendly numbers says that two numbers are called friendly if each is equal to
the sum of the proper divisors of the other. Recall that proper divisors are all the divisors
excluding the number itself. For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20,
22, 44, 55, and 110. The proper divisors of 284 are 1, 2, 4, 71, and 142.
If we represent a pair by (m, n) and the sum of the proper divisors of m and n by σ (m) and σ (n)
respectively, then for (220, 284) we get
σ (m) = σ (220) = 1+2+4+5+10+11+20+22+44+55+110 = 284 = n
σ (n) = σ (284) = 1+2+4+71+142 = 220 = m
It can be seen that each partner in a friendly pair has the power to generate the other, thus
symbolizing mutual compatibility, harmonious friendship, and perfect love. Your task is to
determine if a pair of numbers are friends.
Input
The input to the problem will begin with a positive integer c, which is the number of cases. Each
case will consist of two positive integers, n and m, n < 1,000,000 and m < 1,000,000.
Output
For each case, print out n and m and a message indicating whether or not they are friendly using
the format in the sample output.
Sample input
3
220 284
42 24
284 423
Output corresponding to sample input
220 and 284 are friendly
42 and 24 are not friendly
284 and 423 are not friendly
A Sign of the Times?
All US national parks have signs along their roads stating the elevation of
the road. Everglades National Park is no exception, although it is a bit
lower than most of the others. So it has a sign about halfway between the
welcome station and Flamingo with the information “Rock Reef Pass:
Elevation 3 feet”. This sign is uncharacteristically large, starting exactly 4
feet off the ground and being exactly 6 feet in total height including the
post. Note: the “Elevation 3 feet” refers to ground level. Since this is at an
elevation of 3 feet, the bottom of the post is 3 feet above sea level.
Rock Reef
Elevation 3 feet
Sea level
2 feet
4 feet
3 feet
It has been claimed that the oceans are currently rising at the rate of 1.5 millimeters (mm) per
year. If this is so, in what year will the sign have water touching the bottom of its post (i.e.
ground level)? be completely underwater? What if the oceans rise at a slightly higher rate each
year?
The conversion from millimeters to inches is: 1 millimeter = 0.0393700787 inches.
Input
The input will contain one or more data sets. Each data set will be one line, with two real values,
initial, the initial rise in sea level in mm per year and change, the percentage increase per year,
with initial > 0.0. The file will end with a line containing initial = 0.0 which you should not
process.
Output
Your program must output the case number (starting at 1) and the years in which the water
touches the bottom of the signpost (that is, completely covers the ground) and covers the entire
sign, formatted as shown in the sample output. You should assume that the water is at sea level
in 2008 and increases by the percentage given by initial in 2009, then increases over the 2009
value by the percentage given by change in 2010, and so forth until the level is over the sign.
The water level at the sign is only checked once per year, so even if the water rises above the
sign in a given year, it will not be reported until the sign is checked the next year. You may
assume that the water will always cover the sign at some point in time.
Sample Input
5 0.05
3 0.02
0.0 0.0
Output corresponding to sample input
Case 1: reaches post in 2056, covers sign in 2077
Case 2: reaches post in 2107, covers sign in 2158
Auto Selection
My car radio has a SELECT button that scans the local airwaves and determines the six strongest
FM stations. It then sets the six buttons on the radio to those stations in order of ascending
frequency. If there are fewer than six stations on a scan, the remaining buttons are set to display
the letter A. Once the stations are set, the radio is set to the station that has the strongest signal.
Sometimes the radio can find no stations. In that case, all buttons are set to display the letter A
and the first button is the one that is selected—that is, the first button is set as though that station
had the strongest signal.
Whenever a tie needs to be broken between two or more stations with the same signal strength,
the one with the lowest frequency will be selected. So, if there are two stations with the same
highest signal strength, the one with the lower frequency will be preset.
Write a program that determines what the button settings are for a given set of scan data.
Input
The input consists of one or more sets of scan data. Each set starts with an integer n, 0  n < 30,
the number of stations that my radio scans. There are then n lines, each containing two positive
floating point numbers:
<frequency> <signal strength>
No frequency will appear more than once in a set of scan data.
End of input is indicated by end of file (EOF).
Output
For each scan data, output the data set number starting at 1 (i.e. ‘Set number 1’). On the
following six lines display the frequencies that will be set on the six buttons on the radio, in
order from left to right. The selected frequency is followed by *. If fewer than six stations are
available, an A is displayed. One blank line appears after the output for each data set.
Sample input
3
91.1 14.2
92.3 0.7
90.4 12.2
8
91.1 14.2
92.3 0.7
90.4 12.2
93.1 14.2
103.3 0.2
88.4 13.2
101.1 0.5
99.5 10.4
Output corresponding to sample input
Set number 1
90.4
91.1*
92.3
A
A
A
Set number 2
88.4
90.4
91.1*
92.3
93.1
99.5
Bigger Better Bowling
Mr. Miller, the owner of a prosperous bowling alley, wants to expand to
more bowling-type games. Mr. Miller thinks he can make more money
with a lane that has a variable number of pins. Traditional lanes have
10 pins on four rows (as shown on the right), 1 on the closest row, 2 on
the second row, 3 on the third row and 4 on the fourth row.
To accomplish this, Mr. Miller decided to reduce the size of the pins so
that more than 4 pins would fit across the last row. The new lane will
have a menu that allows bowlers to choose how many rows of pins they would like to play with
and how many pins should fit across the back row. To do this Mr. Miller needs a program that
can take the input from the bowler and return the total number of pins needed so that the pin
dispenser can place the correct number of pins at the end of the lane.
Sometimes a bowler will select a number of rows so that a full row will fit across the lane, as in
the example on the left (six rows and six across the back row). Other times a bowler may want
more rows than pins across the back row, as shown in the example on the right (six rows and five
across the back row).
Input
The input will consist of a list of integers, two per line, rows and pins, 0 < rows < 100,000,
0 < pins < 100,000. rows is the number of rows the bowler wants and pins is the maximum
number of pins that will fit across the lane.
Input will be terminated with a line containing only 0 0, which should not be processed.
Output
For each line of the input (except the terminating case, 0 0), your program should output how
many total pins will be needed in the following format (case-number starts at 1).
case-number: X pins are needed for rows rows.
Sample input
4
6
6
0
6
6
5
0
Output corresponding to sample input
1: 10 pins are needed for 4 rows.
2: 21 pins are needed for 6 rows.
3: 20 pins are needed for 6 rows.
Clocks Going Cuckoo
Unfortunately, the power goes out in my home frequently, often when I’m out. When I get back,
the clocks in my home are in disarray – but I can figure out some things about the power outage
from them.
I have several clocks, which behave differently when the power goes out. In particular:
 My Watch is, of course, completely unaffected.
 My Alarm clock is electronic. When the power comes back on, it resets to midnight, and
keeps time from there.
 My Stove clock is motorized with hands – when the power goes out, it stops at that time,
and picks up from that time when the power comes back on.
For example, if the power goes out at 8:30am, and it comes back on at 9:30am, and I get home at
noon, then:
 My Watch reads 12:00pm
 My Alarm clock reads 2:30am
 My Stove clock reads 11:00am
The Problem
Given the times on these three clocks, determine the time that the power outage started, the time
that it ended, and the length of the outage in minutes. You may assume that there was exactly
one power outage, that it lasted at least 1 minute and no more than 24 hours, and that the clocks
will be consistent. Note that my Stove clock, despite having hands, is still a 24-hour clock, not a
12-hour clock.
Input
The first line of input will have a single integer n, indicating the number of cases to follow. On
each of the next n lines, there will be three times separated by a single space:
Watch Alarm Stove
Each time will be in this format:
hh:mm[am|pm]
Where:
 hh is the hours, 1 <= hh <= 12. Hours less than 10 will NOT have a leading zero.
 mm is the minutes, 00 <= mm <= 59. Minutes less than 10 WILL have a leading zero.
 [am|pm] is either the string “am” or the string “pm”, always in lower case.
There will be no spaces in a time.
Output
For each case, print a single line:
Day number day: start end duration
where day is the number of the case in the input set, start is the start time of the power
outage, end is the end time, and duration is the duration of the outage. The times start and
end should be printed in exactly the format described in the input section. The duration
should be an integer representing the number of minutes that the power was out. There should be
no blank lines in the output.
Sample Input
2
12:00pm 2:30am 11:00am
5:00pm 4:00am 3:00pm
Output corresponding to sample input
Day number 1: 8:30am 9:30am 60
Day number 2: 11:00am 1:00pm 120
Dice Going Clockwise
Regulation Dice are cubes, with each of the numbers 1 through 6 on exactly one face.
Additionally, to be Regulation, the numbers on opposite faces of the die must total 7. So, the 1
must be opposite the 6, the 2 must be opposite the 5, and the 3 must be opposite the 4.
With these constraints, on any Regulation die, it is guaranteed that the 1 face, the 2 face and the
3 face must all meet at some vertex. If, looking at that vertex, the numbers 1, 2, 3 go Clockwise
around the vertex, the die is called a Clockwise die. Otherwise, it is called a Counterclockwise
die.
A Clockwise Die
A Counterclockwise Die
The Problem
Given the faces of a die, determine if it is Clockwise, Counterclockwise or Not Regulation.
Input
Each line of input will have six integers, in this order:
Front Back Left Right Top Bottom
The integers are guaranteed to be in the range 1 through 6, except for the last line, which will be
all 0’s, will mark the end of input, and should not be processed.
Front
Top
Left
Back
Bottom
Right
Output
For each case, print a single line containing the die number (starting at 1) and one of the
following:
Clockwise or Counterclockwise or Not Regulation
There should be no blank lines or extraneous spaces in the output.
Sample Input
1
1
1
1
0
6
6
2
1
0
4
5
3
1
0
3
2
4
1
0
2
3
5
1
0
5
4
6
1
0
Output corresponding to sample input
Die
Die
Die
Die
1:
2:
3:
4:
Clockwise
Counterclockwise
Not Regulation
Not Regulation
Desired Drug Dosing
For years, drugs have been prescribed with dosages like 400 milligrams (mg) every 4 hours. But
why every 4 hours? Perhaps it would be better for patients to have the drug every 217 minutes,
but asking sick patients to worry about timing like that is too difficult.
Fortunately, we live in a computer age and can use programs to determine when drugs should be
administered. All substances have a biological half-life, the amount of time it takes the body to
remove half of the substance from the body. This assumes that the drug is removed at an
exponential rate, so after 1 half-life, only ½ of the drug would be left, after 2 half lives, only ¼ of
the drug would be left, and, in general, for i, any non-negative value (possibly rational!), after i
1
half-lives, only i of the drug would remain in the patient's body. (In fact, the real rate at which
2
drugs are removed is much more complicated, but we will only consider the biological half-life
for this problem.)
Consider a drug with a half-life of 40 minutes. If an initial dosage of 400 mg was given, after 40
minutes, the body would remove half of the drug, so only 200 mg would be left. After 60
1
minutes, there would be 1.5 (or approximately 0.35356) of the initial dosage or approximately
2
1
141.42 mg. After 4 hours, there would be 6 of the initial dosage, or 6.25 mg left. If the person
2
took another 400 mg 4 hours after the first, there would then be 406.25 mg in the patient's
system.
Given the times and levels of previous drug administrations, a drug's biological half-life, and the
desired minimum level, your program should determine the earliest time at which the patient has
a remaining dosage that is less than or equal to the minimum desired dosage.
Input
Input to your program will be one or more data sets.
Each data set will begin with two lines of drug information. The first will have the name of the
drug, a string of 1 or more characters. The second will have two positive integers. The first is
the size (in mg) of one dose and the second is the biological half-life, in minutes.
After the drug information, there will be one or more patient sets. Each patient set will begin
with 2 lines, the first with the patient name, a string of 1 or more characters, and the second with
two positive integers, the desired minimum level (in mg) for the patient, and the second the
number of previous doses, n. There will then be n lines describing the previous doses in the
form:
day:hour:minute doses
where day, hour, minute, and doses are all integers, with 0 ≤ day < 100, 0 ≤ hour < 24, 0 ≤
minute < 60, and 0 < doses. These lines will be in chronological order.
The last patient set will be followed by the line containing just "LAST PATIENT". The last data
set will be followed by the line containing just "LAST DRUG".
Output
For each data set, print a single line with the name of the drug and then one line of output from
each patient set. For each patient’s data set, print the patient's name and the earliest time after
the last dose at which the patient will be below the minimum level. Minutes and hours should
always be printed with 2 digits. Use the format in the sample output below.
Sample input
Problem Example
400 40
Patient 1
10 1
0:10:20 1
Patient 2
50 5
0:10:00 1
0:10:40 2
0:11:20 1
0:12:00 1
0:12:40 2
LAST PATIENT
Easy Drug
100 60
Needs high level
100 2
0:01:00 1
0:02:00 1
LAST PATIENT
LAST DRUG
Output corresponding to sample input
Problem Example
Patient 1 0:13:53
Patient 2 0:15:45
Easy Drug
Needs high level 0:02:36
Divisible Dot Product
The dot product of two vectors is found by multiplying their elements component wise and
summing all the results. So, if v and w each have n elements, then
v · w = v1w1 + v2w2 + v3w3 + … + vnwn
Consider the vectors v = (2, 1, 3) and w = (6, 2, 4). The dot product is 26 (since
26 = 2*6 + 1*2 + 3*4).
A permutation of a vector is a rearrangement of its elements. Mathematically, v’ is a permutation
of v if and only if
v’ = <vp1, vp2, vp3, … , vpn>,
where 1  pi  n, and pi  pj whenever i  j.
Notice that permutations will usually change the dot product. Although v · w in the previous
example is not divisible by 7, the dot product of the permutation w' = (4, 2, 6) and v is 28, which
is divisible by 7.
Given two integers n and m and two vectors v and w, each with n elements, find the
lexicographically least permutation of w, call it w’, such that v · w’ is divisible by m. A vector x
is lexicographically less than a vector y if there is an index i such that xi < yi and xj = yj for all j <
i.
Input
The input will consist of one or more test cases. The first line of each test case will contain two
integers n and m, where 1  n  15 and 1  m  200. The next two lines will each consist of n
positive integers less than 10,000 separated by single spaces, representing the elements of v and
w, respectively.
The last test case is followed by the line “0 0”. This line should not be processed.
Output
The output will be one line for each input case. Each line should indicate the case number and
then n integers separated by single spaces, representing the lexicographically least permutation
w’ of w such that v · w’ is divisible by m. If no such permutation exists, print the line “No such
permutation.”
Sample input
3
2
6
3
1
2
0
7
1
2
2
1
3
0
3
4
1
4
Output corresponding to sample input
1: 4 2 6
2: No such permutation.
ISBN Conversion
Most books have a unique number assigned to them called an International Standard Book
Number (ISBN). Until January 1, 2007, these were ten-digit codes, where the first nine digits
identified the book and the tenth digit was used as a checkdigit. Old checkdigits were any digit
or the letter X, representing the number 10. The algorithm used to compute checkdigits for tendigit ISBNs is not important here. For example, Kernighan and Pike's "The Practice of
Programming" has the ten-digit ISBN 020161586X.
In 2007, a new thirteen-digit ISBN was introduced. All existing ten-digit ISBNs are converted to
thirteen-digit ISBNs by adding the prefix of 978. Again, the ISBN's last digit represents a
checkdigit.
The checkdigit of a thirteen-digit ISBN is computed by computing the sum of multiplying each
digit in the ISBN by a weighting factor. The weighing factor starts at 1 for the most significant
digit and alternates between 3 and 1 for each subsequent digit. Take the remainder of the
weighted sum when divided by 10. If this remainder is zero, the checkdigit is zero, otherwise the
checkdigit is the result of subtracting this remainder from 10.
The first twelve digits of "The Practice of Programming”'s new thirteen-digit ISBN's are
978020161586. The checkdigit for this book would be computed as follows.

Sum the digits multiplied by the weighting factor:
9*1 + 7*3 + 8*1 + 0*3 + 2*1 + 0*3 + 1*1 + 6*3 + 1*1 + 5*3 + 8*1 + 6*3 = 101

Take the remainder of the sum when divided by 10:
101 modulo 10 = 1

Since the result is non-zero, subtract this remainder from 10:
10 - 1 = 9
Therefore, the thirteen-digit ISBN for "The Practice of Programming" is 9780201615869.
Write a program to convert from ten-digit ISBNs to thirteen-digit ISBNs.
Input
The first line of input contains a single integer n, (1≤ n < 10,000), representing the number of test
cases to follow. Each of the following n lines will represent a valid ten-digit ISBN.
Output
For each of the n ten-digit ISBNs, output a line number (starting with 1), a colon, a space, the
original ten-digit ISBN, a colon, a space, and the converted thirteen-digit ISBN.
Sample Input
3
020161586X
0394800168
0977616606
Output corresponding to sample input
1: 020161586X: 9780201615869
2: 0394800168: 9780394800165
3: 0977616606: 9780977616602
Who is My Friend?
(N.B. If this description looks familiar, you're right!)
February is the month of love and friendship. Numbers can be friends too. It is
said that when Pythagoras was asked "What is a friend?", he replied that a friend
is one "who is the other I such as are 220 and 284."
One definition of friendly numbers says that two numbers are called friendly if each is equal to
the sum of the proper divisors of the other. Recall that proper divisors are all the divisors
excluding the number itself. For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20,
22, 44, 55, and 110. The proper divisors of 284 are 1, 2, 4, 71, and 142.
If we represent a pair by (m, n) and the sum of the proper divisors of m and n by σ (m) and σ (n)
respectively, then for (220, 284) we get
σ (m) = σ (220) = 1+2+4+5+10+11+20+22+44+55+110 = 284 = n
σ (n) = σ (284) = 1+2+4+71+142 = 220 = m
It can be seen that each partner in a friendly pair has the power to generate the other, thus
symbolizing mutual compatibility, harmonious friendship, and perfect love. Your task is to find
a number’s friend or to state that a number has no friend.
Input
The input to the problem will begin with a positive integer c, which is the number of cases. Each
case will consist of a single positive integer, n. All integers will be positive numbers less than
9,223,372,036,854,775,808. (This value is the largest representable by the native integers in
Java and C++.)
Notice, this value is larger than the default integer maximum given in the general instructions.
This makes the problem a bit more challenging.
Output
For each case, if the value n has a friend, print out n and its friend using the format in the sample
output. If the number n does not have a friend, print n and a message as shown in the sample
output.
Sample input
3
220
42
9173867825962788039
Output corresponding to sample input
220 has the friend 284
42 has no friend
9173867825962788039 has the friend 9181665478703733561