1996 LSU Computer Science Programming Contest - Novice Problems

Novice #1: Kaprekar's Process

Take any number and arrange its digits in descending order and in ascending order and subtract. Repeat with the result, repeat with the result, etc. This is called "Kaprekar's process."

For example, suppose the number is 132. Rearranging the digits into descending and ascending order gives 321 and 123. Subtract & repeat:

321 - 123 = 198
981 - 189 = 792
972 - 279 = 693
963 - 369 = 594
954 - 459 = 495
954 - 459 = 495 and the calculation repeats endlessly.

The number 495 is Kaprekar's constant for 3-digit numbers, the eventual result of Kaprekar's process applied to any 3-digit number (apart from the exceptional numbers whose digits are all equal). The number 132 from the example above took 5 steps to reach 495.

Write a program which repeatedly prompts for and interactively reads single integers. The input number may be invalid in three ways:

For case A, the program should terminate and print

GOODBYE. 

For cases B and C, print INVALID NUMBER ENTERED.

(Do not terminate the program for cases B & C.) If the number entered is valid, the program should print how many steps of Kaprekar's process is required to reach 495, i.e.

THE NUMBER 132 REQUIRED 5 STEP(S) TO REACH 495. 

(Note: The number 495 is considered to require 1 step of the process to reach itself.)

Sample run:

ENTER A POSITIVE 3-DIGIT NUMBER: 132
THE NUMBER 132 REQUIRED 5 STEP(S) TO REACH 495.
ENTER A POSITIVE 3-DIGIT NUMBER: 222
INVALID NUMBER ENTERED.
ENTER A POSITIVE 3-DIGIT NUMBER: 1234
INVALID NUMBER ENTERED.
ENTER A POSITIVE 3-DIGIT NUMBER: 495
THE NUMBER 495 REQUIRED 1 STEP(S) TO REACH 495.
ENTER A POSITIVE 3-DIGIT NUMBER: -1
GOODBYE.

Novice #2: Telephone Number

On the keypad of a telephone, each number from 2-9 is associated with three letters as follows:

2 ABC
6 MNO
3 DEF
7 PRS
4 GHI
8 TUV
5 JKL
9 WXY

The numbers 1 and 0 do not have letters on a telephone keypad, but for purposes of this problem, let the associations be:

1 Q
0 Z

Using these number-letter associations, one can take any alphabetic string and translate it into its corresponding telephone number. For example, the string 'GQPWXAJ' would translate to the phone number 417-9925.

Write a program which processes an unknown number of character strings. You may assume that each character string will consist entirely of the capital letters A-Z.

The program should convert each input line into its corresponding numeric equivalent. The output for valid input lines should begin with the phrase "THE NUMERIC EQUIVALENT IS: " and be followed by a number formatted as follows:

If the line contains: Format the number like this:

7 characters 123-4567
10 characters (123) 456-7890
11 characters 1-234-567-8901

If a character string beginning with an asterisk is entered, the program should terminate and print "GOODBYE." If a string of length other than 7, 10, or 11 characters is entered (except for a string beginning with an asterisk), the program should print "INVALID DATA ENTERED." and prompt for the next string. Assume that all input strings will contain at least one and no more than 80 characters. Strings will not be entered with leading blanks.

Sample run:

ENTER A CHARACTER STRING: GQPWXAJ
THE NUMERIC EQUIVALENT IS: 417-9925
ENTER A CHARACTER STRING: YKKZZAOPTGD
THE NUMERIC EQUIVALENT IS: 9-550-026-7843
ENTER A CHARACTER STRING: DFEUBNTHPQ
THE NUMERIC EQUIVALENT IS: (333) 826-8471
ENTER A CHARACTER STRING: DDD
INVALID DATA ENTERED.
ENTER A CHARACTER STRING: *ABC
GOODBYE.


Novice #3: Adjacent Numbers

Two numbers will be called "adjacent" if they differ by at most 1.

Input: Several sequences of 20 positive integers.

Problem: Find the length of the longest sequence of adjacent numbers for each input sequence. You may assume that there will be exactly 20 positive integers in each sequence. The number of lines used to enter the numbers in a sequence and the number of blanks between numbers should not matter to your program. You may assume that the user will enter only 'Y' or 'N' in response to the continuation prompt.

Sample run:

ENTER A SEQUENCE OF 20 POSITIVE INTEGERS: 7 12 4 16 6 5 4 4 5 14 13 13 9 14 4 15 2 6 19 20
THE LENGTH OF THE LONGEST SEQUENCE OF ADJACENT NUMBERS IS 5.
ANOTHER SEQUENCE (Y/N)? Y
ENTER A SEQUENCE OF 20 POSITIVE INTEGERS: 4 36 52 61 35 36 35 36 37 83 82 83 42 21 67 93 36 37 91 80
THE LENGTH OF THE LONGEST SEQUENCE OF ADJACENT NUMBERS IS 5. ANOTHER SEQUENCE (Y/N)? N
GOODBYE.

(Note: In the first sample above, the sequence itself is: 6 5 4 4 5. In the second sample above, the sequence itself is: 35 36 35 36 37.)


Novice #4: Rotating Substitution Cipher

Write a program which accepts two character strings as input:
  1. 1. A sequence of lowercase letters (called KEY) such that the sequence is exactly 26 letters long and each letter of the alphabet appears in the sequence exactly once.
  2. 2. A plain text message (called MESSAGE) consisting of 50 or fewer lowercase letters (no blanks or punctuation marks). You may assume that the two input strings will be valid according to the rules described. Once the input strings have been read, the program should encrypt MESSAGE using KEY according to the following procedure. If the first letter in MESSAGE is the 5'th letter in the alphabet, the encrypted version of the MESSAGE replaces it with the 5'th letter in KEY. Do the same for each succeeding letter, except that as you encrypt each letter in MESSAGE, rotate each letter in KEY one space to the right (i.e. the first letter in KEY becomes the second letter, the second letter becomes the third, etc. ... and the 26'th letter becomes the first). For each run, your program should accept one KEY and one MESSAGE and produce as output one encrypted message.

Sample run #1:

ENTER KEY: qwertyuiopasdfghjklzxcvbnm
ENTER MESSAGE: dog
ENCRYPTED MESSAGE: rft
Sample run #2:

ENTER KEY: mnbvcxzlkjhgfdsapoiuytrewq
ENTER MESSAGE: proteus
ENCRYPTED MESSAGE: apfpmaf


Novice #5: Moirre Triangles

This problem involves drawing what may be loosely termed a Moirre triangle, where the word "Moirre" refers to the type of pattern we will generate inside of the triangle. Input to the program is interactive. The initial prompt is 'INPUT A TRIANGLE (Y/N)?'. If Y is entered, prompt for data with 'ENTER DATA:'. Once the triangle has been printed, ask if the user wants to print another with the prompt 'INPUT ANOTHER TRIANGLE (Y/N)?'. Each line of data entered by the user will consist of four positive integers followed by three characters. There will be exactly one blank between all input values.

The program should draw one isosceles triangle per input line. Two blank lines should be left between the last line of output for a triangle and the subsequent prompt. The first integer will be the width of the page (1 <= pagewidth <= 80). The second integer will be the height of the triangle (guaranteed small enough so that its base may be printed in pagewidth or less characters). The triangle drawing must be centered with its peak in the middle of the page. If the triangle may not be exactly centered, there should be one more empty column to the left of the base of the triangle than to the right of it. The third and fourth integers will be, respectively, the number of consecutive background characters and the number of consecutive foreground characters to use in the drawing (see sample output for clarification). The three characters will be the symbol used to draw the edges of the triangle, the background char- acter, and the foreground character, in that order. Before printing the triangle, echo-print the data in the form shown in the sample run. Below the triangle should be displayed a scale, of the form shown below, equal in length to the pagewidth.

Sample run (note that the peak of the triangle is in column 40, giving 20 empty columns to the left of the base and 19 to the right of it):

INPUT A TRIANGLE (Y/N)? Y
ENTER DATA: 78 20 3 8 * . o
PAGE WIDTH = 78
TRIANGLE HT = 20
NUM BACK CHRS = 3
NUM FORE CHRS = 8
EDGESYMBOL = *
BACKGROUND = .
FOREGROUND = o
                   *
                  *o*
                 *ooo*
                *oooo.*
               *..ooooo*
              *ooo...ooo*
             *ooooo...ooo*
            *ooooo...ooooo*
           *ooo...oooooooo.*
          *..oooooooo...oooo*
         *oooo...oooooooo...o*
        *ooooooo...oooooooo...*
       *oooooooo...oooooooo...o*
      *ooooooo...oooooooo...oooo*
     *oooo...oooooooo...oooooooo.*
    *..oooooooo...oooooooo...ooooo*
   *ooo...oooooooo...oooooooo...ooo*
  *ooooo...oooooooo...oooooooo...ooo*
 *ooooo...oooooooo...oooooooo...ooooo*
***************************************
123456789 123456789 123456789 123456789 123456789 123456789 123456789 12345678
INPUT ANOTHER TRIANGLE (Y/N)? N
GOODBYE. 

Novice #6: Age of Aquarius

The planets of the solar system travel in approximately circular orbits more or less in a plane perpendicular to the sun's axis of rotation. The farther away a planet is from the sun, the longer its orbit. The orbital periods (time it takes to complete one orbit) of the planets (in years) are:

Mercury 0.24084
Venus 0.61515
Earth 1.00004
Mars 1.8808
Jupiter 11.862
Saturn 29.456
Uranus 84.07
Neptune 164.81
Pluto 248.53

Write a program to keep track of the relative positions of the planets over time. Given as input a number of years from the beginning of time (a positive integer), output the position in degrees (0-360) of each of the nine planets. Planets should be listed in the order shown in the sample run. Output positions should be rounded to the nearest whole degree. Assume that all the planets were created at time zero at position zero degrees and that orbits are perfect circles. An input of zero years indicates the end of the input.

Sample Run:

ENTER NUMBER OF YEARS: 100 
MERCURY 77 
VENUS 202 
EARTH 359 
MARS 61 
JUPITER 155 
SATURN 142 
URANUS 68 
NEPTUNE 218 
PLUTO 145 
ENTER NUMBER OF YEARS: 0 
GOODBYE.


Novice #7: The Pharoah's Pyramid

Pyramids are built by layering cubic blocks 1 unit on a side in the following manner: The top layer contains 1 block, the next layer down contains 4 blocks (2 by 2) the next layer down contains 9 blocks (3 by 3) etc... Each layer in the complete pyramid will be a perfect square. The work crew has collected N blocks at the work site. The Pharaoh is in a hurry and wants to know how many layers he can have in his pyramid if they stop collecting blocks now and use only what they have already gathered (note that there may be some blocks left over).

The Pharaoh wants his pyramid to have an inner chamber, also pyramid shaped, built under the same constraints as exist for the pyramid itself, left hollow in the center of the pyramid. This will free up extra blocks such that it might be possible to build a larger pyramid than if the pyramid were solid. In order to house such an inner chamber, the outer pyramid must be at least 2 layers taller than the inner chamber, so that the top-center 1x1x1 hollow space that is the top level of the inner chamber will fit into the space normally taken by the center block of the third layer from the top of the pyramid.

Note: The pyramid and the inner chamber may share the bottom layer and still be legal. In other words, the inner chamber is considered to be "contained" by the pyramid even if they both start "at ground level" so to speak. Example: A 3 level high pyramid with a 1 level high chamber would be legal. Write a program that accepts as input the number of blocks available. Output should be a table listing all possible combinations of pyramid height and inner chamber height that could actually be constructed under the construction guidelines given above. The program should prompt repeatedly for a number of blocks, printing a table for each input number. When a negative value is entered for the number of blocks available, the program should terminate and print "GOODBYE.".

Sample run:

ENTER NUMBER OF BLOCKS AVAILABLE: 13
LEGAL COMBINATIONS OF PYRAMID AND INNER CHAMBER HEIGHT: 
Pyramid Height: 3 Inner Chamber Height: 1 
Pyramid Height: 2 Inner Chamber Height: 0 
Pyramid Height: 1 Inner Chamber Height: 0 
Pyramid Height: 0 Inner Chamber Height: 0 
ENTER NUMBER OF BLOCKS AVAILABLE: 42 
LEGAL COMBINATIONS OF PYRAMID AND INNER CHAMBER HEIGHT: 
Pyramid Height: 5 Inner Chamber Height: 3 
Pyramid Height: 4 Inner Chamber Height: 0 
Pyramid Height: 4 Inner Chamber Height: 1 
Pyramid Height: 4 Inner Chamber Height: 2 
Pyramid Height: 3 Inner Chamber Height: 0 
Pyramid Height: 3 Inner Chamber Height: 1 
Pyramid Height: 2 Inner Chamber Height: 0 
Pyramid Height: 1 Inner Chamber Height: 0 
Pyramid Height: 0 Inner Chamber Height: 0 
ENTER NUMBER OF BLOCKS AVAILABLE: -1 
GOODBYE.