Programming Team: Practice Contest, 1-Feb-05
Mt Lebanon HS 2004-5
David Kosbie
Link to the Programming
Team Home Page.
This is a Quick Contest. Work as individuals, not in
teams. Good luck!
- Write a program which reads in two positive integers, s and n,
and prints out (as a percentage rounded to the nearest tenth) the
probability of two rolls of s-sided dice summing to the value n.
Sample input:
6
7
Sample output:
16.7%
(This value is output because, with s==6 and n==7, we are seeking
the odds of rolling two 6-sided dice and summing to 7 -- that is,
rolling a 6+1 or 5+2 or 4+3 or 3+4 or 2+5 or 1+6, which is 6/36, or
1/6, which is 0.166666.... = 16.7% when rounded.)
- Write a program which reads in a non-negative integer n and
prints out 13 + 23 + ... + n3.
- Leonard Euler, one of the greatest math minds ever, proved the
following:
π2/6 = 1/12 + 1/22 + 1/32
+ 1/42 + 1/52 + ...
And so:
π = (6
(1/12 + 1/22 + 1/32 + 1/42
+ 1/52 + ... ))1/2
Astonishing! Write a program which reads in a double
"epsilon", and prints out the smallest integer k such that the first
k terms of this series (that is, summing up to and including 1/k2)
lead to an estimate of pi to within epsilon of the actual value.
- The Maclaurin series for arctan(x) is a formula which allows us
to compute an approximation to arctan(x) as a
polynomial in x. The formula is:
arctan(x) = x - x3/3 + x5/5 - x7/7
+ x9/9 - x11/11 + . . .
Write a program which reads in a double "x" and another double
"epsilon", and prints out the smallest integer k such that the first
k terms of this series compute the value of arctan(x) within epsilon
of the actual value (which can be computed with Math.atan(x)).
- Write a program which reads in a non-negative integer n followed
by a second non-negative integer k (where 0 <= k <= n), and prints
out the number of ways you can choose k items from among n total
items. We call this "n-choose-k", and the formula for
"n-choose-k" is: n! / (k! * (n-k)!). So, for example:
7-choose-2 = 7! / (2! * 5!) = 7*6/2 = 21.
This means that there are 21 ways to choose 2 items from 7 total
items.
- Write a program which reads in a non-negative integer n and
prints out n! -- that is, n factorial, which is n * (n-1) * ... * 2
* 1.
- Write a program which reads in an integer in [0,100) and prints
out the number of nickels that will be returned when making
appropriate change for that many cents. So, if the user enters, say,
73, this represents 73 cents, which would be made as 2 quarters, 2
dimes, and 3 pennies, so your program would output 0, since no
nickels are used here.
- Write a program which reads in three doubles -- the lengths of
the sides of a triangle -- and prints out the area of the triangle,
rounded to the nearest hundredth. Note that if the three sides
cannot form a triangle, then you must print out "impossible" instead
of the area.
- Write a program which reads in two integers -- the hour and
minute in military time (so hours run from 0 to 23, minutes from 0
to 59) -- and prints out the same time in standard time, using
"a.m." and "p.m." appropriately, and without any leading 0's in the
hour, and with the minutes always containing exactly two digits.
| Sample Input: |
3
17 |
23
5 |
| Sample Output: |
3:17 a.m. |
11:05 p.m. |
- Write a program which reads in
four doubles -- the slope "m1" and y-intercept "b1" of one
line, then the slope "m2" and y-intercept "b2" of a second line.
Your program should print out the y-intercept of a third line which
is perpendicular to the first line and which intersects the first
line at the same point where the second line intersects the first
line. If the first two lines do not intersect, or if the third
line could not have a y-intercept, then output "impossible".