B1 - Comparison of Areas
You are given three values:
1) the radius of a circle (r);
2) the height of a triangle (h); and
3) the base of that triangle (b).
You are to determine which shape is larger in area, the circle or the triangle .
The area
of a circle is determined by the following formula :
A = pr2
(you may assume that p = 3.1416)
The area of a triangle is determined by the following formula:
A = h b / 2
You may assume that all values will be positive, and that the areas of the two
shapes
will be different.
Example 1:
Enter radius: 2.5
Enter height: 5.0
Enter base: 3.0
The circle is larger.
Example 2:
Enter radius: 2.0
Enter height: 5.6
Enter base: 7.8
The triangle is larger.
B2 – Roman Numerals
Given a string that correctly represents a Roman Numeral,
print out the integer value. Assume that the
string is less than 14 characters.
Example 1:
Enter Roman Numeral: MCMXCVIII
answer is 1998
Example 2:
Enter Roman Numeral: CDLIX
answer is 459
B3 - Double Arrow
This program will draw a double arrow using the '*'
character. The left arrowhead should
look like :
*
*
***
*
*
and the right arrowhead should look like:
*
*
***
*
*
The shaft should contain n *'s, where n is given as input. You may assume that n
is
greater than 0 and less than 74.
Example 1:
Enter n: 1
* *
* *
*******
* *
* *
Example 2:
* *
* *
***********
* *
* *
B4 -- Difference in words
This program should count the number of differences
between two words of the same
length. Each difference is a position i such that the character at position i in
one
word is different from the character at position i in the other word. You may
assume
that the two words given as input are of the same length, are of length at most
15, and
consist of only alphabetic characters.
Example:
Enter first word: computers
Enter second word: commuting
There are 4 differences.
B5 -- Reducing Fractions
The input for this program consists of two positive,
non-zero integers, which represent
the numerator and denominator of a fraction. The output is the fraction reduced
to
lowest terms .
Example:
Enter numerator: 18
Enter denominator: 12
The reduced fraction is: 3/2
B6 – Tic Tac Toe
Write a program that plays tic-tac-toe. The user moves
first. Accept user moves as an integer from 1 to
9. The computer can play any open space. Do not allow the user to play in a
space already occupied.
Print the board after each computer move. Identify wins by either X or O.
Example:
Enter move: 1
X O
Enter move: 4
X O O
X
Enter move: 4
That space is already occupied, enter another move: 9
X O O
X O
X
X wins
Problems - Advanced
A1 -- Nim
You are to write a program that plays the version of Nim given by the following
rules:
- The game is played with a certain number of stones (given as input).
- The computer and the player alternate taking 1-4 stones. The computer plays
first
- The one who takes the last stone wins.
A winning strategy is to leave a number divisible by 5 (if
possible). Your program
should use this strategy whenever it is possible. If this strategy is
impossible, the
program should take 1 stone.
You may assume that the initial number of stones is
positive and that the player cheats.
Example 1:
Enter initial number of stones: 21
I take 1 stones.
There are 20 stones.
How many will you take? (1-4) 3
That leaves 17 stones.
I take 2 stones.
There are 15 stones.
How many will you take? (1-4) 2
That leaves 13 stones.
I take 3 stones.
There are 10 stones.
How many will you take? (1-4) 1
That leaves 9 stones.
I take 4 stones.
There are 5 stones.
How many will you take? (1-4) 4
That leaves 1 stones.
I take 1 stones.
I win!
Example 2:
Enter initial number of stones: 10
I take 1 stones.
There are 9 stones.
How many will you take? (1-4) 4
That leaves 5 stones.
I take 1 stones.
There are 4 stones.
How many will you take? (1-4) 4
Congratulations -- you win!
A2 – Roman Numerals
Given two strings that correctly represent Roman Numerals,
print out the Roman Numeral that
represents the difference between the two numbers. Assume that the first number
is the larger
number. Assume that each string is less than 14 characters.
Example 1:
Enter first Roman Numeral: MCMXCVIII
Enter second Roman Numeral: ML
answer is CMXDVIII
Example 2:
Enter first Roman Numeral: CDLIX
Enter second Roman Numeral: CDLI
answer is VIII
A3 – Cross Word Puzzles
Given four, 2-character words, try to arrange them into a
2 by 2 cross word. If they fit, print out
the solution . If there is not an arrange, print out an error message.
Example 1:
Enter first word: ab
Enter second word: ab
Enter third word: aa
Enter fourth word: bb
answer is
ab
ab
Example 2:
Enter first word: ab
Enter second word: ab
Enter third word: ab
Enter fourth word: aa
no solution
A4 – Trains
Train A leaves town X heading south and train B leaves
town Y heading east. The two trains will both
use a section of track 100 km south of town X and 150 km east of town Y. The two
trains cannot be on
the shared track at the same time. The two trains go opposite directions on the
shared track. Train A is
1 km long, Train B is 2 km long.
Given the suggested departure times and speeds , determine
which train should be delayed and by how
many minutes to avoid meeting on the shared track. Minimize the amount of the
delay. Enter start
times in hr:min. Assume a 24 hour clock. All times will be on the same day.
Enter speeds in km/hr.
example 1:
enter start time of train A
13:59
enter start time of train B
14:02
enter speed of train A
100
enter speed of train B
150
no delay necessary
example 2:
enter start time of train A
10:12
enter start time of train B
10:08
enter speed of train A
50
enter speed of train B
75
delay train B by 8 minutes
A5 -- Polynomial multiplication
You are to write a program that multiplies two polynomials
of one variable and prints
the result. The polynomials are represented by a sequence of coefficients. The
first
coefficient represents the highest- order term , and successive coefficients
represent
successively lower- order terms . Thus, the sequence
4 -2 0 1
Represents the polynomial
4x3 - 2x2 + 0x + 1
You may assume that all coefficients are integers, and
that the two given polynomials
have degree at most 15. Be sure the coefficients are listed (in both input and
output)
in the order specified.
Example:
Enter coefficients of first polynomial:
4 -2 0 1
Enter coefficients of second polynomial:
1 2 3
Coefficients of result:
4 6 8 -5 2 3
The nth element is obtained from the (n-1)st element by reading the (n-1)st. For
example, the first element contains, "One 1"; hence, the next element is 11. The
fifth
element is, "Three 1's, two 2's, one 1"; hence the sixth element is 312211.
You may assume that the number generated contains no more than 1000 digits.
Example 1:
Enter n: 1
1
Example 2:
Enter n: 6
312211
Example 3:
Enter n: 17
11131221131211132221232112111312212321123113112221121113122113111231133221121321
132132211331121321231231121113122113322113111221131221
A6 -- Conway's sequence
You are to write a program to determine the nth element in
the following sequence:
1, 11, 21, 1211, 111221, ...
The nth element is obtained from the (n-1)st element by reading the (n-1)st. For
example, the first element contains, "One 1"; hence, the next element is 11. The
fifth
element is, "Three 1's, two 2's, one 1"; hence the sixth element is 312211.
You may assume that the number generated contains no more than 1000 digits.
Example 1:
Enter n: 1
1
Example 2:
Enter n: 6
312211
Example 3:
Enter n: 17
11131221131211132221232112111312212321123113112221121113122113111231133221121321
132132211331121321231231121113122113322113111221131221