2000springcontest.cs.mercer.edu/MUSpring2006.doc · Web viewIf this sum is divisible by 3, then n...

45
Tricks (prog1.cpp or prog1.java) Problem Description There is a simple trick to see if any number n is divisible by 2 – just look at the last digit. If the last digit is divisible by 2, then n is divisible by 2. This same trick will work for 5 and 10 as well. There is a simple trick to see if any number n is divisible by 3 – just add up the digits. If this sum is divisible by 3, then n is divisible by 3. This same trick also works for 9. There are many such tricks – but they can become complicated. For example, the trick for 6 is to take the 1’s digit of n as it is, and then multiply all the other digits (10’s, 100’s etc.) of n by 4, and add them all up. The result is divisible by 6 if and only if n is divisible by 6. For 7, there is a sequence of six integers which repeats. Multiply the first (1’s) digit of n by 1, the second (10’s) digit of n by 3, the next (100’s) by 2, then 6, then 4, then 5, then 1, then 3, etc. The resulting sum is divisible by 7 if and only if n is divisible by 7. Define a Trick for an integer k>1 as an infinite sequence a of integers such that: Every term a i in the sequence is between 0 and k-1, inclusive If you take any number n, and multiply its lowest digit (1’s place) by a 1 , its next lowest (10’s place) by a 2 , and so on, then add up all of those, the resulting sum is divisible by k if and only if n is divisible by k. At some finite point in the sequence a, a finite subsequence of terms repeats infinitely. If we use parentheses to wrap the repeating part, then: The trick for k=2 looks like this: 1 (0) The trick for k=3 looks like this: (1) The trick for k=6 looks like this: 1 (4) The trick for k=7 looks like this: (1 3 2 6 4 5)

Transcript of 2000springcontest.cs.mercer.edu/MUSpring2006.doc · Web viewIf this sum is divisible by 3, then n...

Page 1: 2000springcontest.cs.mercer.edu/MUSpring2006.doc · Web viewIf this sum is divisible by 3, then n is divisible by 3. This same trick also works for 9. There are many such tricks –

Tricks(prog1.cpp or prog1.java)

Problem DescriptionThere is a simple trick to see if any number n is divisible by 2 – just look at the last digit. If the last digit is divisible by 2, then n is divisible by 2. This same trick will work for 5 and 10 as well.There is a simple trick to see if any number n is divisible by 3 – just add up the digits. If this sum is divisible by 3, then n is divisible by 3. This same trick also works for 9.

There are many such tricks – but they can become complicated. For example, the trick for 6 is to take the 1’s digit of n as it is, and then multiply all the other digits (10’s, 100’s etc.) of n by 4, and add them all up. The result is divisible by 6 if and only if n is divisible by 6. For 7, there is a sequence of six integers which repeats. Multiply the first (1’s) digit of n by 1, the second (10’s) digit of n by 3, the next (100’s) by 2, then 6, then 4, then 5, then 1, then 3, etc. The resulting sum is divisible by 7 if and only if n is divisible by 7.

Define a Trick for an integer k>1 as an infinite sequence a of integers such that:

Every term ai in the sequence is between 0 and k-1, inclusive

If you take any number n, and multiply its lowest digit (1’s place) by a1, its next lowest (10’s place) by a2, and so on, then add up all of those, the resulting sum is divisible by k if and only if n is divisible by k.

At some finite point in the sequence a, a finite subsequence of terms repeats infinitely.

If we use parentheses to wrap the repeating part, then:The trick for k=2 looks like this: 1 (0)The trick for k=3 looks like this: (1)The trick for k=6 looks like this: 1 (4)The trick for k=7 looks like this: (1 3 2 6 4 5)

Given a selection of integers k, compute the trick for each.

Page 2: 2000springcontest.cs.mercer.edu/MUSpring2006.doc · Web viewIf this sum is divisible by 3, then n is divisible by 3. This same trick also works for 9. There are many such tricks –

Input (standard input)Each line of input will have a single integer k, 2 <= k <= 105, with no spaces. The input will terminate with a 0 on its own line, which should not be processed.

Output (standard output)For each integer k in the input, compute its trick. Display each trick on its own line, in the format described above (i.e. with parentheses around the repeating part). There should be no spaces at the beginning or end of a line. Put a single space between integer terms, and between an integer and an opening parenthesis. An opening parenthesis should have no spaces after it. A closing parenthesis should have no space before it, and should terminate the line. There should be no blank lines.There are cases where the same trick may be represented in many ways. For example, the trick for k=3, which is “(1)”, might also be represented as “1 (1)”, or “(1 1 1)”, or even “1 1 1 (1 1 1 1 1)”. In such a case, only the shortest representation will be considered to be correct. Thus, for k=3, “(1)” is the only correct answer.There are also cases where there appears to be more than one trick, but it’s really the same trick multiplied by a constant. For example, “(2)” will also work for k=3. Clearly, this is the same as “(1)”. In such a case, only the trick with the smallest first term will be considered to be correct. Thus, “(2)” will not be accepted for k=3.

Sample Input23456789100

Sample Output1 (0)(1)1 2 (0)1 (0)1 (4)(1 3 2 6 4 5)1 2 4 (0)(1)1 (0)

Page 3: 2000springcontest.cs.mercer.edu/MUSpring2006.doc · Web viewIf this sum is divisible by 3, then n is divisible by 3. This same trick also works for 9. There are many such tricks –

Train Crash!(prog2.cpp or prog2.java)

Problem DescriptionYour program will help the train conductor determine how far ahead he must put on the brakes to stop his train before it crashes through a barrier! Train stopping distances may be determined by the number of cars they pull. Each car creates additional inertia to retard the efforts of the brakes. This inertia causes an addition to the total stopping distance for each new car on the train. The additional distance for each car is a percentage of the distance before that car was added. This problem is highly hypothetical – do not try to drive your train with these rules in mind!

ExamplesFor example, if the stopping distance for a single car is 10 feet and the added distance for each additional car is 50% of the previous distance, then for two cars this train requires 15 feet to stop, for three cars 22.5 feet, for four cars 33.75 feet, and so forth.

Input (standard input)Your program will, of course, handle multiple trains. Each train will be defined on a separate line and consist of three positive integers: total number of train cars, stopping distance for a single car, and the percentage increment for each additional car. After the last train’s data will be a line with a single zero to indicate end of input.

Output (standard output)For each train from the input you will print one line consisting of “Train #number:” followed by the calculated distance required to stop the train (rounded to two decimal places) followed by “feet required”. After the last train’s distance, you should output on a separate line “Done.”

Sample Input3 10 5010 10 5025 5 2515 30 1530 10 250

Sample OutputTrain #1: 22.50 feet requiredTrain #2: 384.43 feet requiredTrain #3: 1058.79 feet requiredTrain #4: 212.27 feet requiredTrain #5: 6462.35 feet requiredDone.

Page 4: 2000springcontest.cs.mercer.edu/MUSpring2006.doc · Web viewIf this sum is divisible by 3, then n is divisible by 3. This same trick also works for 9. There are many such tricks –

Runny Babbit(prog3.cpp or prog3.java)

Problem DescriptionRunny Babbit is the title of a billy sook by Shel Silverstein. Some of the crinteresting itters in the book are Rirty Dat, Kiddle Litten and her Mom Calley At and Poppy Slig. Your job is to take a dictionary of correctly spelled English words and use it to convert a bory from the stook into a more understandable version – in correct English.

[Runny Babbit is the title of a silly book by Shel Silverstein. Some of the interesting critters in the book are Dirty Rat, Liddle Kitten and her Mom Alley Cat, and Sloppy Pig. Your job is to take a dictionary of correctly spelled English words and use it to convert a story from the book into a more understandable version – in correct English.]

Rules and ExamplesFor this program, a word is a consecutive sequence of letters and the apostrophes. Words must begin with a letter. All dictionary words will be entirely in lower case, but they must match their capitalized form if that occurs in the story. For example, “Runny” in the story matches “runny” in the dictionary.

All words that appear in the final version of the text should appear in the dictionary. If there is a word that is in the original text that does not appear in the dictionary, there will be another word, later in the original text, possibly separated by other words, white space, and punctuation, that is not in the dictionary but can be paired with the first word. This pair of words can have their first 0, 1, or 2 consonants reversed; giving a pair of words that is in the dictionary. You do not need to remove the same number of consonants from each word (so you may exchange the first consonant from the first word with the first 2 consonants from the second). If a word starts with 2 consonants, you must exchange both of them. Similarly, you cannot select to exchange 0 consonants from a word that begins with one consonant.

If a word is capitalized, its replacement should be as well. If not, then its replacement must not be (No words will contain upper case letters anywhere other than the first position.). For example, assuming a standard English dictionary:

“A ced rat” becomes “a red cat” “trimbing up a clee” becomes “climbing up a tree”. “bory stook” becomes “story book” “Calley At” becomes “Alley Cat” “today Miles won at rath” becomes “today Riles won at math.”

All words to be changed will be in pairs and one pair will be completed before another begins, though there may be other words separating the pair. It is not necessary to make changes to words other than those required for the spelling check. For example, changing “at” to “cat” does not require a preceding “an” to be changed to “a”.

Page 5: 2000springcontest.cs.mercer.edu/MUSpring2006.doc · Web viewIf this sum is divisible by 3, then n is divisible by 3. This same trick also works for 9. There are many such tricks –

Input (standard input)Your program will contain one or more input sets.Each set will begin with a dictionary of acceptable words. These will be listed one per line. After the dictionary, there will be a line consisting of just two asterisks ("**"). There will then be a story of one or more lines to fix. You may assume that this story can be fixed. After the story, there will be a line consisting of just two asterisks.

The end of input will be indicated by a dictionary with no words. This set should not be processed.

Output (standard output)The output for each set will be the translated story as defined by the rules above. You should use the same line structure as in the input in the output, changing just the leading consonants of words not in the dictionary. There should be a blank line after each story.

Page 6: 2000springcontest.cs.mercer.edu/MUSpring2006.doc · Web viewIf this sum is divisible by 3, then n is divisible by 3. This same trick also works for 9. There are many such tricks –

Sample Inputgoodfun**foodgood fungun**mammaaandcoatfullhatholesitmadematchnewnoofonepatchrabbitrunny'stotryusewas**Runny's Cat and HoatRunny's hoat was full of colesNo use to try and patch it.Ramma Mabbit made a new oneAnd a mat to hatch it.******

Sample Outputgoodgood funfun

Runny's Hat and CoatRunny's coat was full of holesNo use to try and patch it.Mamma Rabbit made a new oneAnd a hat to match it.

Page 7: 2000springcontest.cs.mercer.edu/MUSpring2006.doc · Web viewIf this sum is divisible by 3, then n is divisible by 3. This same trick also works for 9. There are many such tricks –

Oil Spill(prog4.cpp or prog4.java)

Problem DescriptionOil spills are catastrophic. One thing that scientists can do is to anticipate the magnitude of a spill. When oil is spilled in one spot in the water, the oil rapidly contaminates a large connected area. The size of the contaminated area depends upon land barriers, and the length of time of the spill. For our purposes, when oil spills on land, we will not calculate the effects.

For this problem, we have a rectangular map divided into grid cells of 10 miles on each side, each representing 100 square miles. Each unit is completely composed of either water (W) or land (L). We will assume that once a spill has occurred on water, the spill continues to spread outward until it reaches land. In our simple model, in one day oil can move from one unit of water to the next unit of water if the units are connected on the left, right, above, below, or diagonal. For example, consider the following map where land is represented with an “L” and water is represented with a “W” (inverted color used to help visualize, not part of the input), and the rows are indexed top-to-bottom from 1..10 and the columns left-to-right 1..15.

L L L L L L L L L L L L L L LL L W W W L W W W W W W W W LL L W L L W W W W W L L W W LL L L L W W L W W W L L W W LL W L W W W L W W W L L W W LL W L W W L L W W W L W W W LL L L W W W W W W L L L L L LL L W W L W W W L L L W W W LL L L L L L L L L W W W W W LL L L L L L L L L L L L L L L

If an oil spill took place in row 5, column 6 (marked with a 1 below) after 9 days, 4400 square miles would be contaminated, as illustrated in this map. The characters “L” and “W” have been omitted to assist you in visualizing the oil spill. Notice that the numbers indicate the day number on which the oil first reaches each square.

6 5 4 4 4 5 6 7 8 96 3 3 4 5 6 9

2 2 4 5 63 2 1 5 5 63 2 5 6 63 3 3 4 5 6

4 4 4 4 5

Page 8: 2000springcontest.cs.mercer.edu/MUSpring2006.doc · Web viewIf this sum is divisible by 3, then n is divisible by 3. This same trick also works for 9. There are many such tricks –

Input (standard input)For this program you are given T test cases. The first line of the input will be the value of T. Following that line will be a set of input lines for each of the T test cases. The first line of each test case will have 5 integers: rows, cols, r, c, and d. Rows is the number of rows in the map, cols the number of columns in the map, r, c, the row and column of an oil spill, and d, a number of days (0 < d). After that line will be rows lines representing the map. Each line will contain cols letters, either 'L' representing land or 'W' representing water. The outermost units of each map are always all L’s.

Output (standard output)Your program should output the case number (as shown below), starting with 1, followed by the appropriate one of these three options:

1. If (r,c) hits water, output the number of square miles of water contaminated after d days labeled as shown below.

2. If (r,c) hits land, output “Hits land.”3. If (r,c) is off the map, output “Invalid coordinates.”

Use the format given in the Sample Output below.

Page 9: 2000springcontest.cs.mercer.edu/MUSpring2006.doc · Web viewIf this sum is divisible by 3, then n is divisible by 3. This same trick also works for 9. There are many such tricks –

Sample Input410 15 5 6 9LLLLLLLLLLLLLLLLLWWWLWWWWWWWWLLLWLLWWWWWLLWWLLLLLWWLWWWLLWWLLWLWWWLWWWLLWWLLWLWWLLWWWLWWWLLLLWWWWWWLLLLLLLLWWLWWWLLLWWWLLLLLLLLLLWWWWWLLLLLLLLLLLLLLLL3 3 2 2 1LLLLWLLLL3 3 1 2 1LLLLWLLLL3 3 3 4 5LLLLWLLLL

Sample OutputCase 1: 4400 square miles.Case 2: 100 square miles.Case 3: Hits land.Case 4: Invalid coordinates.

Page 10: 2000springcontest.cs.mercer.edu/MUSpring2006.doc · Web viewIf this sum is divisible by 3, then n is divisible by 3. This same trick also works for 9. There are many such tricks –

Flat Robot World(prog5.cpp or prog5.java)

Problem DescriptionGiven the dimensions of a flat, rectangular world and a set of robots that can operate in that world, write a program to simulate robot movements for a specified duration of time to determine their final positions and orientations.A world is a rectangular grid laid out as a Cartesian plane with x and y coordinates. The bottom left location is the location at coordinates (0, 0). Each robot in the world has a unique identification number and is at a specific grid location (x, y). More than one robot can occupy the same location. Each robot has an orientation that describes the direction it is moving: north, south, east, or west. Each robot also has a program that it executes that controls its movement and possibly the movement of other robots. A program is a sequence of instructions. The instructions are shown in the table.

A program has at least one instruction and a valid program must contain at least one H or T instruction. A program can contain at most 32 instructions.For example, a robot with the program FRT will move forward, turn right, and then reset to turn right again, thus moving clockwise inside a two-by-two square in the world. Another with FFFRT will move clockwise around inside a four-by-four square. Another with program RFNNNT will move slowly around inside a two-by-two square.

Since the world is flat, it is possible for a robot to move off the edge. A robot that moves off the edge is lost and cannot continue program execution.

Simulated time is discrete. At each tick of the simulation clock, each robot executes one instruction. (Each robot keeps track of which of its instruction is executed next.) The effect is that all robots execute instructions in parallel and at the same time. In particular, if a robot executes an @ instruction, locations of other robots are determined at the start of the clock tick. If a robot is reset by another robot at tick number t, then the reset takes effect at tick t+1; the robot being reset still executes its current instruction at time t as though it has not been reset.

Instruction Description

R Rotate right to change orientation, but remain at the current grid location.

F Move forward one grid location in the direction of its current orientation.

N No operation—that is, do not move forward or rotate.

TReset—that is, start execution at the first instruction with no change of orientation or location.

H Halt. Stop executing instructions (until reset).

@ Reset all of the other robots in the same grid location.

Page 11: 2000springcontest.cs.mercer.edu/MUSpring2006.doc · Web viewIf this sum is divisible by 3, then n is divisible by 3. This same trick also works for 9. There are many such tricks –

Input (standard input)The program reads sets of simulation data, each set being a group of lines as defined below.

The first line of a set specifies the dimensions of the world, width followed by height. The maximum width is 50 and the maximum height is 50. If the width is specified as zero or the length is specified as zero, then the program stops processing input.

The second line of a data set specifies the number N of robots in the world. There will be at least 1 robot and no more than 100 robots in any simulation.

The third line specifies T, the number of time steps in a simulation. Assume T > 0.

Each of the next N lines specifies a robot in the simulation—an ID number (a non-negative integer), a location (x-coordinate followed by y-coordinate), an orientation (N, E, S, or W for north, east, south, or west), and then a program that is a string of instructions that extends to the end of the line. All values on a line are separated by a single space character.

You may assume that all robot locations are initially within the world and that orientation is a valid value. IDs are listed in ascending order, but need not be sequential.

Output (standard output)For each set of simulation data, the program displays the results of the simulation: a data set number and then the state of each robot in that simulation on each of the next N lines in the format

ID# status

where status is either (x, y) orientation if the robot is still in the world or LOST if the robot fell off the edge. The robots should be listed in increasing ID number order—that is, the same order they were read in.

After the last set of simulation results, the program displays DONE on a line by itself.

Page 12: 2000springcontest.cs.mercer.edu/MUSpring2006.doc · Web viewIf this sum is divisible by 3, then n is divisible by 3. This same trick also works for 9. There are many such tricks –

Sample Input10 10471 0 0 S FT2 9 8 W FFRT3 5 6 W RH4 3 6 E FF@FT5 5350 0 0 E F@@T1 0 0 E FNRT2 0 0 E FRNT10 204101 0 0 N FT2 9 19 W FRRRT4 5 10 S FFRFFRRRT6 5 10 S HRRRT0 10

Sample OutputData Set #1:1 LOST2 LOST3 (5, 6) E4 (8, 6) EData Set #2:0 (2, 0) E1 LOST2 LOSTData Set #3:1 (0, 6) N2 (9, 18) E4 (3, 6) S6 (5, 10) SDONE

Page 13: 2000springcontest.cs.mercer.edu/MUSpring2006.doc · Web viewIf this sum is divisible by 3, then n is divisible by 3. This same trick also works for 9. There are many such tricks –

Crazy Computer Science Professor(prog6.cpp or prog6.java)

Problem Description

A deranged and highly overworked computer science professor gets rid of some of his freshmen students’ assignments by a strange "Russian Roulette" grading process. He starts out by having his students submit their program solution files into a prepared submission folder. He then runs an unusual grading script which deletes various programs apparently at random. However upon very close inspection one can determine that there is really a method to this apparent randomness. The script starts on some random file in the folder. It then selects a magic number, x, which is used to determine which program file gets the dangerously determined deletion. The magic number is counted (count from one, not zero) and the file on which the counting ends is the unlucky collection of bits! Once that file is selected its bits are forever transferred into the great “bit bucket in the sky” never more to be seen in the submission folder again. The process repeats itself by counting the magic number starting from the next file to select the subsequent dangerously determined deletion. The process continues until the number of files to be disposed of has been reached. The students whose files avoid the bit bucket receive a grade of A on the assignment. NOTE: The script treats the list of files as though they were in a circular list.

Consider the situation where the starting position is 6 and the magic number is 14. All counting is done starting with 1, so the first file to be deleted is determined by counting 14 from file 6:

index 1 2 3 4 5 6 7 8 9 10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

value 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

count 1 2 3 4 5 6 7 8 9 10

11

12

13

14

The value in position 19 is then selected for deletion. If another program is to be deleted, the count starts with the next item (in this case, item 20):

index 1 2 3 4 5 6 7 8 9 10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

value 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1

count 121314

1 2 3 4 5 6 7 8 9 10

11

Page 14: 2000springcontest.cs.mercer.edu/MUSpring2006.doc · Web viewIf this sum is divisible by 3, then n is divisible by 3. This same trick also works for 9. There are many such tricks –

So the value in position 3 will be deleted. If another program is to be deleted, the count starts with the next item (in this case, item 4):

index 1 2 3 4 5 6 7 8 9 10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

value 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1

count 1 2 3 4 5 6 7 8 9 10

11

12

13

14

So the value in position 17 will be deleted. If another program is to be deleted, the count starts with the next item (in this case, position 18):

index 1 2 3 4 5 6 7 8 9 10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

value 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1

count 1314

1 2 3 4 5 6 7 8 9 10

11

12

Here, there is no longer an item in position 19, so it is skipped in the count and the item in position 2 is deleted. If one more item is to be deleted, the count starts in position 4 (since the item in position 3 is deleted):

index 1 2 3 4 5 6 7 8 9 10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

value 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1

count 1 2 3 4 5 6 7 8 9 10

11

12

13

14

Here, element 18 is deleted and the count restarts at position 20, if more elements are to be deleted.

In order to stagger his unbearable work load, the professor has 30 students at a time turn in an assignment. Each assignment consists of submitting 1 source code file into the crazy professor’s submission folder. The professor chooses the number of files to be removed each assignment (based upon his teaching and publishing loads) and the position where the counting begins (counting does not always begin at the same position each assignment). The magic number (from 1 to 30) is chosen at random. Given the file number where the counting started, and the end results, your job is to determine the magic number.

Input (standard input)

Page 15: 2000springcontest.cs.mercer.edu/MUSpring2006.doc · Web viewIf this sum is divisible by 3, then n is divisible by 3. This same trick also works for 9. There are many such tricks –

The input contains several assignments worth of data. Each assignment is stored on its own line and will contain the position number where the counting started followed by a series of 30 numbers (0's and 1's) which describes the fate of the files based on their original submission order: 0 means the student’s file was deleted; 1 means the student received an A. Each integer is separated by one space. Continue processing until you encounter a line that has a single zero in the first column. Do not process this final line.

Output (standard output)Output the line number (start at 1), followed by a “:” followed by a space and then the calculated magic number. If there is more than one possible magic number, print the smallest one.

Page 16: 2000springcontest.cs.mercer.edu/MUSpring2006.doc · Web viewIf this sum is divisible by 3, then n is divisible by 3. This same trick also works for 9. There are many such tricks –

Sample Input

5 1 0 1 1 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 6 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0

Sample Output1: 4 2: 14

Page 17: 2000springcontest.cs.mercer.edu/MUSpring2006.doc · Web viewIf this sum is divisible by 3, then n is divisible by 3. This same trick also works for 9. There are many such tricks –

The Luhn Formula(prog7.cpp or prog7.java)

Problem DescriptionThe Luhn formula, also known as Mod 10, is a simple checksum algorithm used on credit card numbers. It is applied in three steps:

1. Take every other digit, starting with the rightmost, and add them up into a grand total.

2. Take each remaining digit, and double it. If the result is less than 10, add it to the grand total. If the result is greater than or equal to 10, add the digits of the result, and then add that to the grand total.

3. If the grand total is divisible by 10, the number passes, otherwise it fails.

Here’s an example. Consider the number 1393046745572483:

Because the grand total (70) is divisible by 10, the number 1393046745572483 passes.

Note that simply because a sequence of digits passes the Mod 10 test does NOT mean that it’s a valid credit card number – the Luhn formula is just a simple test to screen out random numbers, or mistyped numbers, before accessing a credit card service.

Input (standard input)Each line of input will have a sequence of between 1 and 100 digits, with no spaces. End of the input will be signified by EOF.

Output (standard output)For each sequence of digits, print the digits, a single space and either PASS or FAIL on its own line. The output should have no other spaces or blank lines.

Sample Input1393046745572483123

Sample Output1393046745572483 PASS123 FAIL

3 3 4 7 5 7 4 3254 86091

Step 1: 3+3+4+7+5+7+4+3=36

Step 2: 2+(1+8)+0+(1+2)+8+(1+0)+4+(1+6)=34

254 86091 3 3 4 7 5 7 4 3

Grand Total: 36+34=70

Page 18: 2000springcontest.cs.mercer.edu/MUSpring2006.doc · Web viewIf this sum is divisible by 3, then n is divisible by 3. This same trick also works for 9. There are many such tricks –

Lexicon(prog8.cpp or prog8.java)

Problem DescriptionLexicon is a popular board game similar to Scrabble and Word Slinger where you often find yourself unscrambling potential words to form anagrams. Anagrams are words spelled with the exact same letters as another word. For example, “rides” is an anagram of ”sired" and vice versa. Your task is to write a program that can unscramble words and help you win your next game of Lexicon.

Input (standard input)The input contains four parts: 1) a dictionary, which consists of at least one and at most 100 words, one per line; 2) a line containing XXXXXX, which signals the end of the dictionary; 3) one or more scrambled “words” that you must unscramble, each on a line by itself; and 4) another line containing XXXXXX, which signals the end of all input. All words, including both dictionary words and scrambled words, consist only of lowercase English letters and will be at least one and at most 80 characters long. (Note that the sentinel XXXXXX contains uppercase X's.) The dictionary is not necessarily in sorted order, but each word in the dictionary is unique.

Output (standard output)For each scrambled word in the input, output “Input word: the-scrambled-word” followed by an alphabetical list of all dictionary words that can be formed by rearranging the letters in the scrambled word. Each word in this list must appear on a line by itself. If the list is empty (because no dictionary words can be formed), output the line "NOT A VALID WORD" instead. In either case, output a line containing six asterisks to signal the end of each list.

Page 19: 2000springcontest.cs.mercer.edu/MUSpring2006.doc · Web viewIf this sum is divisible by 3, then n is divisible by 3. This same trick also works for 9. There are many such tricks –

Sample InputtarpgivenscorerefundonlytrapworkearncoursepepperpartXXXXXXresconfudreaptrsettoresucXXXXXX

Sample OutputInput word: rescoscore******Input word: nfudrerefund******Input word: aptrparttarptrap******Input word: settNOT A VALID WORD******Input word: oresuccourse******

Page 20: 2000springcontest.cs.mercer.edu/MUSpring2006.doc · Web viewIf this sum is divisible by 3, then n is divisible by 3. This same trick also works for 9. There are many such tricks –

Image Processing(prog9.cpp or prog9.java)

Problem DescriptionOne important application of CS is processing digital images. In this program, you will take a grayscale PGM image file as input, enhance the contrast range of the image, and output the resulting PGM image file to the screen. When the PGM files are viewed they could look like:

Input PGM file Output PGM file

Your program should be able to accept an image of up to 1024x1024 pixels in size. Process the input as follows: An input image may be given with pixel values that differ only slightly (low contrast), such as in the range from 100 to 175 or even from 30 to 45. By stretching the range of values to cover the range from 0 to 255, a much higher contrast version of the image is created. Your job is to find the minimum and maximum grayscale values in an image and remap the values between them to cover the range from 0 (min) to 255 (max). The minimum pixel value in the original image will be changed to 0 and the maximum pixel value will be changed to 255. All other pixel values will be changed so they maintain the same relative distance from the minimum and maximum value, rounding as necessary. For example, if the original image had a pixel range from 100 to 120, all 100s in the original image would appear as 0s in the enhanced image and all 120s in the enhanced image would appear as 255s in the enhanced image. A pixel value of 110, halfway between 100 and 120 would map to 128 (since 127.5 is halfway between 0 and 255 and 127.5 rounds to 128). Output the information in the same format as the original, with the new higher contrast range pixel values. Input (standard input)The input file consists of one or more images in PGM format.The PGM format is a simple text-based image file format and has the following standards:

The first lines hold the header of the image. A sample header may be:P2

# Created by Paint Shop Pro 7 64 64 255 The first line is the PGM type, which is a string with no embedded characters (P2 in this

case). There is then a comment line (or lines) beginning with the # symbol. Next is a line with the number of columns and number of rows of the image in pixels

separated by a space (which will always be positive integers, except in the last case where we use zeros to signal end of input).

The last line of the header is a single integer with the maximum grayscale value (a positive integer less than or equal to 255).

Page 21: 2000springcontest.cs.mercer.edu/MUSpring2006.doc · Web viewIf this sum is divisible by 3, then n is divisible by 3. This same trick also works for 9. There are many such tricks –

Every value that follows these lines is a pixel graymap intensity value between 0 and the maximum grayscale value of the image (0 is black, the maximum value is white, halfway between is medium gray), separated by one or more spaces. Pixel values are given for each pixel in row major format, so all pixels on the first row of the image are listed first, followed by the next row of pixels, etc. In the above example, we have 64 rows of 64 pixels (a 64x64 pixel image) – but note that the end of a line in the text document is not the same as the end of a row; i.e. the text wraps around after each 20 or so values, so there may be many more (or fewer) lines of input. Here are the first 8 pixels of the above image:

162 136 153 158 143 128 122 123

There will be at least two different pixel intensities in each image.

The end of input will be marked by an image of 0 by 0 pixels. This image should not be processed.

Output (standard output)The output should be PGM format image following the same standards as given under the input section above. You should echo the header and have line breaks after each 20 pixels image. There should be exactly one space after each pixel's value. You should have one blank line following each image.

Page 22: 2000springcontest.cs.mercer.edu/MUSpring2006.doc · Web viewIf this sum is divisible by 3, then n is divisible by 3. This same trick also works for 9. There are many such tricks –

Sample InputP2# Created by Paint Shop Pro 74 6255110 115 115 115 115 115 116 117 118 119 120120 120 120 115 115 115 120 120 120 120 118118 120P2# Created by Paint Shop Pro 7# Actuallly not# Created in a text editor6 4255110 15 115 215 183 115 116 117 118 119 120120 20 120 215 175 115 120 120 120 120 118118 120P2# Nothing here!0 0255

Sample Output P2# Created by Paint Shop Pro 74 62550 128 128 128 128 128 153 178 204 229 255 255 255 255 128 128 128 255 255 255255 204 204 255

P2# Created by Paint Shop Pro 7# Actually not# Created in a text editor6 4255121 0 128 255 214 128 128 130 131 132 133 133 6 133 255 204 128 133 133 133133 131 131 133

Find this interesting? You can view PGM files for free (but you don't need to do this during the contest!). Jasc Paint Shop Pro is a free demo download that allows viewing of PGM files; Adobe Photoshop will display PGMs; there are other free utilities on the web for viewing PGM files.

Page 23: 2000springcontest.cs.mercer.edu/MUSpring2006.doc · Web viewIf this sum is divisible by 3, then n is divisible by 3. This same trick also works for 9. There are many such tricks –

Bowling(prog10.cpp or prog10.java)

Problem DescriptionBowling is a game where you have ten pins set up at the end of an alley and your goal is to knock them over with a ball. This game has ten “frames” which are turns for you to knock over as many of the ten pins as possible. Each frame starts with ten pins. A normal frame may have two rolls. If you don’t knock over all ten pins with your first roll, you get a second roll in the frame to try to knock down the remaining pins. During the tenth frame, however, if you knock over all of the pins with the first roll, you get two more rolls. Additionally, in the tenth frame, if your second roll knocks over all standing pins, you get a third roll.

Bowling scores are based on how many pins you knock over. For each pin you knock over you get one point. If within any frame you knock over all ten pins, you receive bonus points. The bonus points, which are in addition to the points for knocking over the pins, are calculated like this:

If your first ball in a frame knocks over all ten pins (called a Strike and symbolized by ‘X’) you get a bonus of the number of pins knocked over during your next two rolls.

If your second ball in a frame knocks over all standing pins (called a Spare and symbolized by ‘/’) you get a bonus of the number of pins knocked over during your next one roll.

If while you are delivering a roll, you cross the foul line (or any one of many other infractions) your roll is considered “Foul,” which is symbolized by “F.” Fouls count as one of your rolls, but a score of zero pins is awarded for that roll.

A Bowling score card (from www.bowl.com) is shown below. This card is used to record the rolls of each frame and to calculate the game’s score.

As you can see there are ten frames (numbered across the top of the card). Each of the score card’s frames has a box in the upper right corner. Here is how you use this score card:

When you bowl a Strike in a frame, you put an X inside the box in the upper right corner of the associated frame.

When your first roll knocks over fewer than 10 pins, you put the number of pins knocked over in the upper left corner for the associated frame.

Page 24: 2000springcontest.cs.mercer.edu/MUSpring2006.doc · Web viewIf this sum is divisible by 3, then n is divisible by 3. This same trick also works for 9. There are many such tricks –

When you have a second roll in a frame, you put your results in the box in the upper right corner for the associated frame. The choices here are:

knocked over all remaining pins (i.e. a Spare): put a ‘/’ knocked no more pins over: put a ‘-‘ knocked over some but not all of the remaining pins: put the number of pins

knocked over on this roll. When you bowl a Foul, put an “F” where the score for that roll would normally go. The bottom of each frame on the scorecard is used to keep a running total of your score

(including bonus points).

In the score card above, the bowler had strikes on the first three rolls. The score for the first roll is computed by adding the number of pins knocked down on the first roll (10) to the bonus, the number knocked down on the next two rolls (20). In the fourth frame, the bowler knocked down 7 pins in the first frame and 2 in the second. Therefore, the points earned for the second frame is computed by adding the number of pins knocked down in the second frame (10) to the bonus, the number knocked down on the next two rolls (17, 10 in the third frame and 7 for the first roll of the fourth frame) giving 27. Running totals are listed at the bottom of each frame so 27 + 30 puts 57 at the bottom of the second frame. The points earned for the fourth frame is just 9, since the bowler earned no bonus (no Strike or Spare). The bottom of frame four holds the running total: 76 + 9 = 85.

Input (standard input)The first line of input will be an integer N indicating how many scorecards are given below (0<N<1000). The next N lines will consist of the results of all of the rolls made during a game (i.e. the information from the scorecard excluding the running totals). This line will consist of only the following characters: { 0 1 2 3 4 5 6 7 8 9 X / - F} and will accurately reflect a complete game.

Output (standard output)For each scorecard in the input you should print out a line like:Game #: ScoreWhere # is the game’s number (start counting at 1).

Sample Input2XXX728/F9X7/9-XX8XXXXXXXXXXXX

Sample OutputGame 1: 180Game 2: 300

Page 25: 2000springcontest.cs.mercer.edu/MUSpring2006.doc · Web viewIf this sum is divisible by 3, then n is divisible by 3. This same trick also works for 9. There are many such tricks –

Flipping Pancakes(prog11.cpp or prog11.java)

Problem Description

In the rush to earn money for spring break, groups of students do many strange tasks. One enterprising group has decided to sell pancakes to earn money. They have come up with a unique method of making sure that all of the pancakes get flipped sufficiently.

The first person will flip all of the pancakes. The second person will start at the second pancake and then flip every other one. The third one will start at the third pancake and flip every third one and so on. Assume there are an unlimited number of students.

We are interested in the number of times that each pancake gets flipped.

The Problem

Given a pair of positive integers m ≤ n. Output the number k, m ≤ k ≤ n, such that the kth pancake is flipped the most in the interval [m, n].

Input (standard input)

Each line will consist of a pair of positive integers, 0 < m ≤ n ≤ 65536. You may also assume n − m ≤ 1000. The input will be terminated by a pair of zeros. This line should not be processed.

Output (standard output)

For each supplied interval, output the line number (start with 1) followed by a “:” followed by a single space followed by the number k that corresponds to the pancake that will be flipped the most in the interval followed by the number of times that it will be flipped. In the event that there is a tie in the interval, only display the first (lowest indexed pancake). There should be a single space between the value k and the number of times it has been flipped.

Sample Input

2 34 920000 201000 0

Sample Output1: 2 22: 6 43: 20020 48

Page 26: 2000springcontest.cs.mercer.edu/MUSpring2006.doc · Web viewIf this sum is divisible by 3, then n is divisible by 3. This same trick also works for 9. There are many such tricks –

Counting the Votes(prog12.cpp or prog12.java)

Problem DescriptionCounting the votes accurately and promptly is critical in elections at various levels. Visualizing the result and displaying it properly is also required in the information era.

Assume that you are hired to write a program that can accurately count election votes as well as provide a visualization of the results. Your program will work for a Country that is divided up into States. Each State will also be divided up into voting precincts. Each precinct runs an election and reports which candidate “wins that precinct.” Each State will have an assigned number of electoral votes, all of which are given to the candidate that wins the most precincts in the State.

Your program will be provided input data that includes a “States map” that represents the geographic areas of the States, a list of candidate names with a corresponding single-digit ID number, a list of State names and their corresponding number of electoral votes, and a “Votes Map” that specifies which candidate has won each precinct.

The “States Map” will be represented by an M by N matrix of characters. Each entry in this matrix represents either a precinct of a State or an uninhabited region. Each State is represented in the matrix by a unique alphabetic character. Dots (.) will be used to represent geographical areas that are uninhabited (such as seas or lakes). It is possible that the area of a State is disjoined such as separated by a lake or that a part of a State is an island in the sea. It is also true that this entire matrix is surrounded by Sea.

The “Votes Map” will also be represented by an M by N matrix of characters that correspond with the “States Map” data (VotesMap[i,j] is information about StatesMap[i,j]). Each entry in this matrix contains either the corresponding precinct’s winning candidate’s single-digit ID or the notation for uninhabited areas (the dot).

A State will be won by a candidate whose ID number appears in more than 50% of the precincts in that State. Assume that no two candidates will win 50% of the precincts in a State. The candidate who wins a State will be awarded all of that State’s electoral votes. The candidate who gets the highest total electoral votes wins the election. However, no winner should be declared if none of the candidates win a simple majority.

To visualize the election result, your program also needs to generate a “Winners Map” that depicts winners of each State. This map should look like the “States Map” with the following exceptions:

1. Alphabetic characters (representing the States) only remain if they are on the State’s border. The border is defined as being an entry in the matrix that touches another State or a Sea on its left, right, top or bottom (not diagonal). Recall that the M by N matrix is sitting in the middle of a great big Sea so that all of the matrix edges are touching the Sea. If the State contains a lake (one or more dots completely surrounded by that State),

Page 27: 2000springcontest.cs.mercer.edu/MUSpring2006.doc · Web viewIf this sum is divisible by 3, then n is divisible by 3. This same trick also works for 9. There are many such tricks –

you should not consider lake edges borders. However, if there exists an entry in the matrix that touches a lake and another State or Sea, then that should be considered on the border.

2. All other alphabetic characters (non border entries for States) should be replaced by the ID of the candidate that won that State.

3. All dots remain.

An example of the three maps with two States (a and b) and two candidates (1 and 2) is shown below.

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbaaaaaabbbbbbaaaaaabb..bbbbbbbbbbbbbbbbbbbbbbbb..

11222221111211122211112211111121222211122222222111222222..212222221112222211122222..

aaaaaaaaaaaaa11111aaaaaaa1111abbbbbba1111ab2222baaaaaab2..2bbbbbbbb222bbbbbbbbbbbb..

States Map Votes Map Winner Map

Page 28: 2000springcontest.cs.mercer.edu/MUSpring2006.doc · Web viewIf this sum is divisible by 3, then n is divisible by 3. This same trick also works for 9. There are many such tricks –

Input (standard input) Your program needs to check to make sure only land areas are labeled with candidate IDs, not the lakes or seas.

The input file will have an integer X (with 1<= X <= 10) on the first line indicating how many vote counting cases will be tested. The rest of the input file will be followed by X parts, one for each of the X cases. Each of these parts shares the same structure as follows:

- The first line contains two integers C (with 2 <= C <= 9) and S (with 2 <= S <= 52), separated by a space, which specify the number of candidates and the number of States, respectively.

- There are C lines of candidate information to follow: each consists of a unique single-digit candidate id and a name (in one word). A space is used to separate the id and the name.

- The next S lines contain State information. Each line has a unique single letter State symbol (a-z, A-Z), a State name (which may contain one space but no integers), and an integer representing the State weight. One space will be used to separate the adjunct parts.

- The next line will contain two integers, M (with 10 <= M <= 100) and N (with 10 <= N <= 100), representing the number of rows and number of columns in the maps, respectively. A space will be used to separate the two numbers.

- Two matrices representing the “State” map and the “vote” map will go next, each filling up M lines. Void units (such as seas or land areas that are not considered) to the left of the “States” will be padded with dots (.), while spaces are used for void units to the right and inside the States (such as lakes). No blank lines will be used in maps.

Output (standard output)For each case defined in the input file, your program should output the following contents as specified below:

- A case heading line indicating the case number, the number of candidates and the number of States specified.

- The case heading line is followed by one line that indicates the result. There are three possible outcomes:

o A winner is recognized, who has won W out of P possible electoral votes. Under this scenario, the output should use the following templateThe winner is <name>. (W/P)

o No winner can be claimed, since no candidate has won more than 50% of all possible votes. For such cases, the following line is expected:No winner can be claimed yet.

- A “winner map” as described on page 1 should then be displayed.- A blank line should be used to separate contents for two adjacent cases.

Page 29: 2000springcontest.cs.mercer.edu/MUSpring2006.doc · Web viewIf this sum is divisible by 3, then n is divisible by 3. This same trick also works for 9. There are many such tricks –

Sample Input22 21 X2 Ya A 10b B 107 12aaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbaaaaaabbbbbbaaaaaabb..bbbbbbbbbbbbbbbbbbbbbbbb..11222221111211122211112211111121222211122222222111222222..212222221112222211122222.. 2 41 Bill2 Boba Georgia 12b Florida 21c Alabama 10d N Calorina 1314 12......dddd.......ddd.dd...cccdddddd..cccaaaaaaaaccccaaaaaaaacccccaaaaaa.cbbbbbbbbbb...bbbbbbbbb........bbbb.........bbbb........bbbb.........bbb..........bb.......b.b........1111..

.....111.11.

..111111222.

.1111122222212111111122222222221111.22222222221...222222222........2222.........2111........1111.........112..........12.......2.1..

Sample OutputCase-1 (2 candidates, 2 States)No winner can be claimed yet. aaaaaaaaaaaaa11111aaaaaaa1111abbbbbba1111ab2222baaaaaab2..2bbbbbbbb222bbbbbbbbbbbb..

Case-2 (2 candidates, 4 States)The winner is Bill. (35/56)......dddd.. .....d11.dd. ..cccdddddd. .c1caaaaaaaac11ca111111acccccaaaaaa. Cbbbbbbbbbb. ..bbbbb222b. .......b22b. ........b22b........b22b.........b2b..........bb.......b.b..