Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Spring Programming Competition The Problem Set 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. Backward Fibonacci Caesar, Phone Home Clustering Combinatorial Fun Digital Roots Isograms It’s Gonna Be Another MAD Year Prime Magic Santa vs. the Grinch The Golf Caddy April 9, 2005 Backward Fibonacci (fib.cpp or fib.java) Background The Fibonacci sequence is formed by having two starting numbers 0 and 1, as the first two terms of the sequence. Each subsequent term is found by adding the two previous terms. F0 = 0; F1 = 1; F2 = F0 + F1 = 0 + 1 = 1; F3 = F1 + F2 = 1 + 1 = 2; F4 = F2 + F3 = 1 + 2 = 3; F5 = F3 + F4 = 2 + 3 = 5. This leads to the sequence 0,1,1,2,3,5,8,13,21, . . . For this problem you will be generating forward and backward sequences of “Fibonacci” numbers from specified values for F0 and F1. Your program should read the first two values of the sequence and the count of n numbers to generate from a file. Your program should then output the next n terms of a sequence using the definition. However, in addition to these n (forward) terms you will assume that the sequence extends in both directions so that you will also generate the n previous terms. See the “Sample Output” below. Input (input file: fib.in) The input file will consist of three integers per line. The first two integers will be the values for F0 and F1. The third integer will be the non-negative count of how many numbers need to be generated in both directions (before F0 and after F1). There will be one space between each integer on each line. The last line of the file will consist of three zeros. Output (to screen) For each line of input that comes before the three zeros, your program should print out the following four lines: First line: the word “For” followed by the first two integers from the input line each separated by a single space. Second line: the word “Prev” followed by a space and then followed by the third integer from the input line. This should be followed by a colon and a space. Finally on this line you should display the n previous integers from the sequence that come before F0 each separated with a comma and a single space. Third line: the word “Next” followed by a space and then followed by the third integer from the input line. This should be followed by a colon and a space. Finally on this line you should display the n next integers from the sequence that follow F1 each separated with a comma and a single space. Fourth Line: Blank line to separate each case. Sample Input 0 1 10 15 18 8 77 78 6 123 199 9 29 31 5 0 0 0 Sample Output For 0 1 Prev 10: -55, 34, -21, 13, -8, 5, -3, 2, -1, 1 Next 10: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 For 15 18 Prev 8: 132, -81, 51, -30, 21, -9, 12, 3 Next 8: 33, 51, 84, 135, 219, 354, 573, 927 For 77 78 Prev 6: 377, -226, 151, -75, 76, 1 Next 6: 155, 233, 388, 621, 1009, 1630 For 123 199 Prev 9: 1, 3, 4, 7, 11, 18, 29, 47, 76 Next 9: 322, 521, 843, 1364, 2207, 3571, 5778, 9349, 15127 For 29 31 Prev 5: -77, 52, -25, 27, 2 Next 5: 60, 91, 151, 242, 393 Caesar, Phone Home (caesar.cpp or caesar.java) Background Roman numerals have become all the rage. You remember Roman numerals, where the numbers 1-10 are represented as: Number Roman numeral Number Roman numeral 1 I 6 VI 2 II 7 VII 3 III 8 VIII 4 IV 9 IX 5 V 10 X Now the classics department of your school is thinking of listing phone numbers using Roman numerals instead of Arabic numerals. The classics department has decided to just translate digits instead of try to represent the phone number as a single number, so the digits 49 will be represented as IVIX, not IL or XLIX. They also know that there is no zero in Roman numerals, so decide to use X to represent zero. So, the number 555-1720 would be listed as VVVIVIIIIX. You, as a computer scientist, realize there could be a real problem with this. Sure, VVVIVIIIIX represents 555-1720. But it could also represent 556-5129 or 556-6119 or 555-4310 or many other numbers. To show the classics department the problem, you decide to write a program to compute how many different 7 digit numbers correspond to a given Roman number. For the purpose of this program, a 7 digit number may have 1 or more leading zeros, so 010-2345 is a valid number and should be counted. Input (input file: caesar.in) The input to your program will be one or more Roman numbers, each consisting of 7 or more of the characters I, V, and X. The last line of input will be a line with exactly 7 X's. This line should not be processed. Output (to screen) For each Roman number, print the Roman number and the count of possible 7 digit phone-numbers it might represent, in the format shown in the sample output below. Have a blank line after each line of output. Sample Input VVVIVIIIIX XIXXXXIII XXXXXXX Sample Output VVVIVIIIIX can represent 19 phone numbers XIXXXXIII can represent 3 phone numbers Clustering (cluster.cpp or cluster.java) Background In real world applications, physical or abstract objects are usually described by a number of attributes. Clustering refers to the process of grouping data into classes or clusters so that objects within a cluster have high similarity in comparison to one another, but are very dissimilar to objects in other clusters. Dissimilarities are assessed based on the attribute values describing these objects. Often, distance values, such as Euclidean distance, are used. Here, we consider a set of points that are defined in a two-dimensional domain. Dissimilarity between two points, Pi ( xi , yi ) and Pj ( x j , y j ) , is then defined as Euclidean distance between them. d (i, j ) x x j yi y j 2 i 2 Cluster is defined as such grouping that the distance between points in a cluster is less than the distance between a point in the cluster and any point outside it. See the following diagram for an example. Since the (best) number of clusters is usually not known, the data analyst may need to provide the number for a clustering algorithm to be executed. Input (input file: cluster.in) The input file will have an integer N (with 1<= N <= 100) on the first line indicating how many clustering cases will be tested. The rest of the input file will be followed by N parts, one for each of the N cases. Each of these parts shares the same structure as follows: - The first line contains two integers P (with K<= P <= 100) and K (with 2<= K <= 10), separated by a space, which specify the number of points and the number of clusters, respectively. - There are P lines of point definitions to follow: each consists of two integers separated by a space, indicating the x and y (where 0<= x or y <= 1000) coordinates for a point, respectively. Output (to screen) 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 and number of clusters specified. - The case heading line is followed by K lines, each of which lists all the points in one of the K clusters. Each of these lines should start with Cluster-x:, with x being the cluster sequence number ranging from 1 to K. it is then followed by point sequence numbers for all points in the cluster. The sequence numbers should be the order in which these points are defined in the input file, ranging from 1 to P. The adjacent sequence numbers should be separated with a tab. - A blank line should be used to separate contents for different clusters. Sample Input 2 6 2 1 1 2 1 2 2 4 3 4 4 5 4 9 3 2 9 1 10 2 2 4 1 3 2 3 3 9 8 9 9 10 8 Sample Output Case-1 (2 clusters) Cluster-1: 1 2 3 Cluster-2: 4 5 6 Case-2 (3 clusters) Cluster-1: 1 2 Cluster-2: 3 4 5 6 Cluster-3: 7 8 9 Combinatorial Fun (combo.cpp or combo.java) Background Consider the following formula: C(n, r) = n! / ((n-r)! * r!) where n and r are non-negative integers and n >= r. Write a program that will read in two integers, n and r, and print an indication of whether or not C(n, r) is even or odd. Recall that 0! = 1! = 1. Input (input file: combo.in) Each line of the input file will contain two integers, n followed by r, each between 0 and 33001 inclusively. The program will continue to execute until either n or r is negative. The last case is not to be processed. Output (to screen) For each pair (n and r) from the input file, you should print them out as “C(n, r) ” followed by either “is even.” or “is odd.” Sample Input 4 2 5 5 -1 0 Sample Output C(4, 2) is even. C(5,5) is odd. Digital Roots (digital.cpp or digital.java) Background The digital root of a positive integer is found by summing the digits of the integer. If the resulting value is a single digit then that digit is the digital root. If the resulting value contains two or more digits, those digits are summed and the process is repeated. This is continued as long as necessary to obtain a single digit. For example, consider the positive integer 24. Adding the 2 and the 4 yields a value of 6. Since 6 is a single digit, 6 is the digital root of 24. Now consider the positive integer 39. Adding the 3 and the 9 yields 12. Since 12 is not a single digit, the process must be repeated. Adding the 1 and the 2 yields 3, which is a single digit and also the digital root of 39. Input (input file: digital.in) The input file will contain a list of positive integers, one per line. The end of the input will be indicated by an integer value of zero. Output (to screen) For each integer in the input, output its digital root on a separate line of the output. Sample Input 24 39 0 Sample Output 6 3 Isograms (isograms.cpp or isograms.java) Background Isograms are words that consist of the same letters used a particular number of times – each letter in the word will be used n times. It is easy to find small words that are isograms. The words dog, cat, house, and fish are 1-letter isograms. However the words inn, that, start, and letter are not isograms. The word dog is a 1-letter isogram, the word tartar is a 2-letter isogram and the word deeded is a 3-letter isogram. Write a program that will read a word from a file and then determine if it is an isogram. If it is not an isogram, output that fact. If it is an isogram indicate whether it is a 1, 2, 3, etc. letter isogram. Input (input file: isograms.in) The input file will have an integer N on the first line indicating how many strings will be tested. The following N lines of the file will consist of the words to be tested. You can assume that the words will be 1-60 characters in length and contain no spaces. Have your program treat uppercase and lowercase letters as identical. Thus taRtAr should be recognized as a 2-letter isogram. The only legal characters in each word should be alphabetic characters. Output (to screen) For each input word your program should output the appropriate output line: “inputString” is not an isogram. “inputString” is a x-letter isogram. (where x is the number of repeats) “inputString” contains an illegal character. After the last input string has been processed, your program should print out the message “TaTa”. Sample Input 5 dog TartAr remove test-test aqwsssaqwaqwswqa Sample Output “dog” is a 1-letter isogram. “TartAr” is a 2-letter isogram. “remove” is not an isogram. “test-test” contains an illegal character. “aqwsssaqwaqwswqa” is a 4-letter isogram TaTa It’s Gonna Be Another MAD Year (mad.cpp or mad.java) Background MAD Magazine’s cover for March 1961 (Issue 61) reflected the symmetry of that year, which reads the same upside down. The digits 0, 1, 6, 8, and 9 have a property that when rotated 180° they can still be read as a digit. With the invention of LEDs and digital clocks, the digits 2 and 5 also possess this property. (Even though an LED “1” is not centered, we will continue to consider it to have the property.) LED Digit Reflection In the age of LED numbers, we potentially have a lot more MAD years. The purpose of this program is to determine how many MAD Years there are in a given range of years, where a year is a MAD year if it reads the same upside down (rotated 180°). Thus, is a MAD year, as are , , , , , and . Input (input file: mad.in) The input file consists of a sequence of lines, each containing two integer values year1 and year2 that represent a range of years. The values are separated by one blank. The smallest year is 1 and the largest year the program will process is 99999. The last line contains two zeros and should not be processed. Output (to screen) The output to the screen should be a sequence of lines that give year1 and year2 followed by the number of MAD years between them, inclusive. More specifically, each line of output lists the start and end years separated by one blank. The end year is followed immediately by a colon, then by a single blank, and then by the number of MAD years in the range of the years specified. Sample Input 1000 1999 2000 2099 1000 9000 0 0 Sample Output 1000 1999: 7 2000 2099: 1 1000 9000: 35 Prime Magic (magic.cpp, or magic.java) Background A magic square is a 2-dimensional array of integers, in which each row, each column, and both diagonals sum to the same value. A prime magic square is a magic square, all of whose entries are prime numbers (plus we additionally allow 1 to be one of the entries). Your job is to detect if the given 2-dimensional array of integers is a prime magic square, just a magic square or neither. Input (input file: magic.in) The input begins with a non-negative integer x, which denotes the number of test cases. Each test case begins with an integer n, followed by n rows of n integers. All array entries are positive integers no greater than 1,000,000, and 1<=n<=100. These n-by-n integers represent the 2-dimensional array to be tested. Output (to screen) For each test case, your program prints out one of not magic magic, not prime prime magic Sample Input 3 2 4 4 4 4 2 1 2 2 1 3 67 1 43 13 37 61 31 73 7 Sample Output magic, not prime not magic prime magic Santa vs. the Grinch (santa.cpp or santa.java) Background It happens every year: Santa has lots of presents for you, but the Grinch wants you to get as few as possible. Every year, the Grinch talks you into playing a game with him. Each year one of the Grinch’s assistants creates a binary tree with a number of presents available at the leaves. On each turn, the player gets to make one of two possible choices. On your turn, you always want to make the choice that will give you the most presents in the end, and when it’s the Grinch’s turn, he will make the choice that gives you the least presents in the end. The situation can be illustrated by the following binary tree, in which leaf nodes show how many presents you get if you can get to that node, starting from the root. You always get to go first, and the Grinch always plays perfectly, so for the tree shown below, the biggest number of presents you can get is four, but only if you make the right choices. Since you get talked into this game every year, you’ve decided to be prepared next year. Write a program that takes as input a nonempty binary tree in which each internal node has two children and leaf nodes contain an integer representing a number of presents. Your program determines the maximum number of presents you can get if you play perfectly. Input (input file: santa.in) The input begins with the integer 1<=n<=1000, the number of test cases, followed by n lines, each containing a tree. The tree is represented in the input in the following syntax: p (1 p 9999) is a tree consisting of one leaf node. The value p represents the number of presents you would receive if you get to this leaf node. <L,R> is a tree with left subtree L and right subtree R Under this scheme, the tree on the previous page is represented as: <<<3,<2,7>>,6>,<<1,4>,5>> Output (to screen) For each test case, print the maximum number of presents you can get if you play perfectly. Each answer should be on a line by itself. Single space your answers. Sample Input 2 <<<3,<2,7>>,6>,<<1,4>,5>> 5 Sample Output 4 5 The Golf Caddy (golf.cpp or golf.java) Background Its springtime and you’ve decided to roll up your sleeves and earn a little money this year as a caddie out at the local golf club. As a caddie, you are responsible for keeping the scorecard for a golf foursome. On the scorecard, the players’ names appear at the top, each heading a column, and then each row will correspond to a hole in the match. The score the player receives on a hole should be placed in that player’s column in the row for the given hole. At each hole, you notice the order in which players tee off. The score of the first person to tee off is placed in the first column, the second person to tee off in the second column, the third person to tee off in the third column, and the last person to tee off in the fourth column. Your scoring method assumes players will always shoot in the same order on every hole as they did on the first hole. In reality, the golfers were rearranging themselves based on their score from the previous hole. The golfer with the lowest score on the previous hole shoots first on the following hole. If two golfers tied on the previous hole, the golfer that went earlier the hole before went earlier on this hole. Unfortunately, you did not realize your scoring mistakes until the 18th hole, and since there was a million dollar bet on the golf round, you need to correct the scores, and fast! Write a program to read in the golf scores that you recorded, and create the corrected score card. Input (input file: golf.in) The first 4 lines will contain the names of the four golfers. The order of the names will be the order they shot on the first hole. Each of the next 18 lines will contain the scores of the four golfers, in the order they shot on that hole. No name will contain more than 10 characters. The maximum score on any given hole will be 20. Output (to screen) Print the scorecard for the golf round, giving each player the score they actually shot. The scorecard should take the following format: The names should appear at the top, all reading down the screen. Each of the names should end on the same line. A line of 35 dashes should appear between the names and the scores from the first hole. Each line should then list the hole, and the score for each golfer, in the appropriate column. After all 18 holes have been listed, another line of 35 dashes should appear. The last line of output should list the total score for each player, in the appropriate column. If 2 or more columns are needed for any number (score, hole or total), the values should be right justified. Sample Input Justin Dave Whit Adam 4 6 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 8 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 Sample Output 2 5 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 6 J u s D W A t a h d i v i a n e t m -----------------------------------1 4 6 3 2 2 3 5 4 4 3 4 4 4 4 4 4 4 4 4 5 4 4 4 4 6 4 4 4 4 7 4 4 4 4 8 4 4 4 4 9 4 4 4 4 10 4 4 4 4 11 4 4 4 4 12 4 4 4 4 13 4 4 4 4 14 4 4 4 4 15 4 4 4 4 16 4 4 4 4 17 4 4 4 4 18 5 6 4 8 -----------------------------------Total 72 77 71 74