University of Scranton 1990 High School Programming Contest Problems

Problem 1: How Many Days?
Problem 2: Digital Roots
Problem 3: Extended Precision Addition
Problem 4: String Subsets
Problem 5: On the Line?
Problem 6: Caesar Cipher Encoding

 


Problem 1: How Many Days?

Write a program that determines the length of time, in days, between two dates given as input. Dates are expressed in the familiar form mm/dd/yy. (The first two digits of the year are assumed to be 19.)

The two dates given as input need not be given in chronological order. However, in the output, the two dates must be listed in chronological order. You may assume that any date given as input corresponds to a day that actually occurred (or will occur). This excludes dates such as 04/31/77 and 02/29/53 (because April has only 30 days and February has only 28 days in non-leap years).

You should be aware that each of January, March, May, July, August, October, and December has 31 days, each of April, June, September, and November has 30 days, and February has either 28 or 29, the latter only in leap years. In the century that includes the years 1900 through 1999, the leap years are all those divisible by four, except for 1900. (Most people are not aware that a year ending in 00 is a leap year only if its first two digits form a number that is divisible by four.)

The program should repeat until the user enters the same date for both inputs.

Sample Program Execution:

1st Date: 04/21/90
2nd Date: 04/24/90
There are 3 days between 04/21/90 and 04/24/90.

1st Date: 04/24/90
2nd Date: 04/21/90
There are 3 days between 04/21/90 and 04/24/90.

1st Date: 03/10/65
2nd Date: 09/21/99
There are 12613 days between 03/10/65 and 09/21/99.

1st Date: 08/11/85
2nd Date: 08/11/85
There are 0 days between 08/11/85 and 08/11/85.

 


Problem 2: Digital Roots

We define the digital root of a nonnegative integer n as follows. Let m be the sum of the digits in n. If m is a one-digit number, then m is the digital root of n. Otherwise, the digital root of n is equal to the digital root of m.

Develop a program that, given as input a nonnegative integer, outputs its digital root. The program should terminate when the user enters 0 as input.

Sample Program Execution:

Enter number: 7
Digital Root: 7

Enter number: 3452
Digital Root: 5

Enter number: 397
Digital Root: 1

Enter number: 0
Digital Root: 0

 


Problem 3: Extended Precision Addition

Develop a program that takes as input two "long" nonnegative integers and outputs their sum. The inputs will be no longer than 35 digits. The program should repeat until the user enters zero for both inputs.

Sample Program Execution:

1st input: 10371
2nd input: 315
Result: 10686

1st input: 54696543201678654789456
2nd input: 82067690004564356875434
Result: 136764233206243011664890

1st input: 0
2nd input: 0

 


Problem 4: String Subsets

A set is a collection of elements, or members. If S and T are sets such that every member of S is also a member of T, then S is said to be a subset of T. (It is possible for each of S and T to be a subset of the other. In this case, they contain precisely the same members.)  If S is a subset of T but T is not a subset of S ---which is to say that every member of S is also a member of T, but T contains some member that is not a member of S--- then S is said to be a proper subset of T.

Given any two sets S and T, they are related to each other ---with respect to the subset of concept--- in exactly one of four ways:

  1. S is a proper subset of T.
  2. T is a proper subset of S.
  3. Each of S and T is a subset of the other.
  4. Neither S nor T is a subset of the other.

With every character string x we can associate the set Sx whose members are precisely those characters appearing (at least once) in x. For example, if x is the string "MISSISSIPPI", then Sx is the set whose members are "M", "I", "S", and "P". If x and y are character strings and Sx is a subset of Sy, we say that x is a string-subset of y.

Develop a program that, given as input two character strings x and y, reports (in the manner illustrated below) which of the four possible relationships holds between the sets Sx and Sy. The program should terminate when the user enters as input two empty strings.

Sample Program Execution:

Enter 1st string: MISAPPROPRIATION
Enter 2nd string: MISSISSIPPI
MISSISSIPPI is a proper string-subset of MISAPPROPRIATION.

Enter 1st string: JUNK
Enter 2nd string: JACKASS
Neither JUNK nor JACKASS is a string-subset of the other.

Enter 1st string: BATTLES
Enter 2nd string: STABLES
Each of BATTLES and STABLES is a string-subset of the other.

Enter 1st string:
Enter 2nd string:

 


Problem 5: On the Line?

Develop a program that, given as input three points P1, P2, and P3 on the cartesian coordinate plane, reports whether P3 lies on the line containing P1 and P2.

In order to input the three points, the user enters six integers x1, y1, x2, y2, x3, and y3. The three points are P1 = (x1, y1), P2 = (x2, y2), and P3 = (x3, y3).

The program should repeat until the user's input is such that P1 and P2 are the same point.

Sample Program Execution:

Enter x1: 12
Enter y1: 5
Enter x2: 7
Enter y2: 9
Enter x3: 10
Enter y3: 7

The point (10,7) IS NOT on the line containing (12,5) and (7,9).

Enter x1: 0
Enter y1: 0
Enter x2: -1
Enter y2: -1
Enter x3: 5
Enter y3: 5

The point (5,5) IS on the line containing (0,0) and (-1,-1).

Enter x1: 7
Enter y1: -2
Enter x2: 7
Enter y2: -2

 


Problem 6: Caesar Cipher Encoding

A Caesar cipher (named after the Roman Emperor Julius Caesar) is a rudimentary encoding scheme in which each letter is replaced by the letter occurring k places after it in the alphabet. (Imagine that the letters are listed in a circle so that A immediately follows Z.) The number k is referred to as the shift because, in effect, a Caesar cipher shifts all the letters of the alphabet k positions.

For example, if k = 2, A would be encoded as C, B would be encoded as D, ..., X would be encoded as Z, Y would be encoded as A, and Z would be encoded as B. The notion of a negative shift makes sense, too. For example, if k = -3, A would be encoded as X, B would be encoded as Y, C would be encoded as Z, D would be encoded as A, ..., Z would be encoded as W.

Develop a program that accepts as input a shift value (an integer) and a message (a string of characters) and that generates as output the corresponding encoded form of the message. The message will be no longer than 40 characters. The shift value will be no less than -25 and no greater than 25. Any character that occurs in the message and that is not an upper case letter should be encoded as itself. The program should repeat until the user enters zero for the shift value.

Sample Program Execution:

Enter shift value: 7
Enter message: THE END IS NEAR
Encoded message: AOL LUK PZ ULHY

Enter shift value: -4
Enter message: ALL ABOARD
Encoded message: WHH WXKWNZ

Enter shift value: 0