C o m p u t e r S c i e n c e D e p a r t m e n t
2005 NJIT High School Programming Contest
Sample Problems
Problem 1
You are asked to compute the GPA (Grade Point Average) of a student for a number of courses
taken by that student. Each student for this problem is a MATH or CS major and takes a
number of Computer Science (CS) courses and a number of Mathematics (MATH) courses.
Two GPAs will be computed: one reflecting the performance of the student in all MATH and CS courses, and one over the courses
only in the student's major.
Input Specification:
The first line of the input will contain an integer N, the number of courses taken by the
student. The second line will contain MATH or CS to indicate whether the student is a
MATH major or CS major. Following the second line, N lines will be included. Each line will
start with a MATH or CS prefix of a course (field 1) followed by a three-digit course
number (field 2) followed by the credit points assigned to this course (field 3), followed by
a letter grade (field 4). Each field will be separated from the neighboring ones by a number
of white characters (space, tab, newline, formfeed). Credit points are single-digit
base-10 numbers. Letter Grades can be A, B, C, D, F. A letter grade A is assigned 4.0 grade
points, B is assigned 3.0, C is assigned 2.0, D is assigned 1.0 and F is assigned 0.0 grade
points. The GPA of a student is the sum of the products of credit points times grade points
divided by total number of credits taken (but not necessarily earned). If a student got an F
in a course, the credit points in that course are used in the GPA computation,
but the student earns no grade points.
Output Specification:
Five lines are printed in the output:
Grade Point Average is x.yz
Grade Point Average in Major is a.bc
Credit Points earned in Major are efg
Credit Points earned are hij
Major is ABCD
x.yz is the GPA over all MATH and CS courses. a.bc is the GPA in the major as specified in
the second line of the input, and ABCD is the major as written in the second line of the
input. The credit points of the major are reported as efg and the total points earned are
reported as hij. If a student receives an F, that student earns no points in that course.
Decimal values are truncated. For example if the GPA is 3.753, then 3.75 is printed.
If the GPA is 3.799999, then 3.79 is printed.
Sample Input
5
CS
CS 113 3 A
CS 114 3 B
CS 435 3 A
MATH 226 4 A
MATH 333 3 F
Sample Output
Grade Point Average is 3.06
Grade Point Average in Major is 3.66
Credit Points earned in Major are 9
Credit Points earned are 13
Major is CS
Problem 2
A positive integer p other than 1 is called a prime number if its only two divisors are 1 and
p (itself). In this problem you are asked to find the n-th prime number for a variety of
values of n.
Input specification:
A sequence of positive integers between 1 and 1000 (inclusive), one such integer per line.
The end of input is indicated by a line that contains a zero (0).
Output specification:
For each line of the input other than the last line
that contains the zero (0), if that line contains positive integer n, output the n-th prime number
in the corresponding output line.
Sample Input
1
5
10
0
Sample Output
2
11
29
The 1-st, 5-th and 10-th prime numbers are 2, 11, and 29 respectively.
Problem 3
This problem deals with temperature conversion for temperatures up to 2003 in various scales.
The user enters in separate lines temperatures followed by a letter indicating the scale.
One line contains the temperature followed by the scale in the following line for that
temperature. The output for the program will be the measurement in all three scales. The input
temperatures are integer. The temperature scale indicator can be a lower- or an upper-case
letter. An F or an f stands for Fahrenheit, C or c for Celsius, and K or k for Kelvin.
The end of file is indicated by a temperature of 2004 degrees. If the entered temperature is
2004, then the program will terminate; in that case a scale is not necessary for 2004 and may
or may not appear as part of the input. Two decimal digits of precision should be printed
for temperatures.
The necessary conversion equations are listed below. Let F, C, and K be degrees Fahrenheit,
Celsius, and Kelvin respectively.
F = (9/5)*C + 32
and
K = C + 273.
Sample Input:
100
c
100
F
2004
Temperature : 100.00
Scale : c
Temperature in F is : 212.00
Temperature in C is : 100.00
Temperature in K is : 373.00
Temperature : 100.00
Scale : F
Temperature in F is : 100.00
Temperature in C is : 37.77
Temperature in K is : 310.77
Program quits
Problem 4
Square roots are easy to find using Newton's approximation formula.
Let us try to find the square root of y. Let x(0), x(1), x(2), x(3) be the
sequence of successive approximations of the square root of y defined as
follows:
x(0) = initial guess
x(i+1) = (1/2) * ((y / x(i)) + x(i)) for i>0.
Both y and x(0) must be positive. The input will be positive real numbers. Write a program
that reads in separate lines y and x(0) and outputs the calculation of x(i+1) for the first
seven iterations, i.e., x(1), x(2), ..., x(7) are printed, and a final answer printing the
current best approximation to the square root of y. Each x(i) must include at least four
decimal digits.
Sample Input:
49.0
49.0
Sample Output:
After iteration 1 : 25.0000
After iteration 2 : 13.4800
After iteration 3 : 8.5575
After iteration 4 : 7.1417
After iteration 5 : 7.0014
After iteration 6 : 7.0000
After iteration 7 : 7.0000
Square root of 49.0 is 7.0000 after 7 iterations.
Problem 5
Given positive integer n, write n in binary notation.
Input Specification:
One positive integer per line is given, with the end of file
indicated by a line containing a 0. Positive integers are as high as 10,000,000. Positive
integers are input without commas and are in base-10.
Output Specification:
For each number of the input, its minimal binary representation is printed; no leading zeroes
should be printed. The 0 of the last line of the input is ignored.
Example Input
15
16
17
44
256
0
Example Output
1111
10000
10001
101100
100000000