Programming and Problem Solving with Java

(やまだぃちぅ) #1
657

Programming Problems


1.Use recursion to solve the following problem.
A palindromeis a string of characters that reads the same forward and
backward. Write a program that reads in strings of characters and determines if
each string is a palindrome. Each string appears on a separate input line. Echo-
print each string, followed by “Is a palindrome” if the string is a palindrome or
“Is not a palindrome” if the string is not a palindrome. For example, given the
input string
Able was I, ere I saw Elba.
the program should print “Is a palindrome.” In determining whether a string is
a palindrome, consider uppercase and lowercase letters to be the same and ig-
nore punctuation characters.
2.Write a program to place eight queens on a chessboard in such a way that no
queen attacks any other queen. This classic problem lends itself well to a recur-
sive solution. Represent the chessboard as an 8 8 Boolean array. If a square is
occupied by a queen, the value is true; otherwise, the value is false. The status
of the chessboard when all eight queens have been placed is the solution.
3.A maze is to be represented by a 1010 array of characters:P(for path),H(for
hedge), orE(for Exit). The maze has one exit. Write a program to determine if it
is possible to exit the maze from a given starting point. You may move
vertically or horizontally in any direction to a square that containsP; you may
not move to a square that containsH. If you move into a square that containsE,
you have exited.
The input data consists of two parts: the maze and a series of starting
points. The maze is entered as 10 lines of 10 characters (P,H, and E). Each
succeeding line contains a pair of integers that represents a starting point (that
is, row and column numbers). Continue processing entry points until end-of-
file occurs.
Free download pdf