
Problem 1 Generation of combinations Level: Hard Given unsigned integers n ≥ k ≥ 0, generate all the k-combinations of the n objects, numbered 1, 2, …, n, using the following algorithm: Algorithm ( k -combinations in lexicographic order) Starting with the combination 1, 2, …, k The next combination is found by scanning the current combination from right to left so as to locate the rightmost element that has not yet attained its maximum value. This element is incremented by one, and all positions to its right are reset to the lowest values possible. Sample test Input: Filename: 1.in Format: There are 10 lines to stand for 10 cases. Each line contains a set of n and k which separated with space. Output: Filename: 1.out Format: 1. For each case, the first line is a prompt as following: “case 1:”. The next lines are all of the combinations. Notice that one line for one combination and list the results in ascending order. 2. You don’t have to print any thing if the result is null. 3. The last line of each case is an empty line, even if the result is null. Example: Input: 0 0 6 3 Output: case 1: case 2: 123 124 125 126 134 135 136 145 146 156 234 235 236 245 246 256 345 346 356 456 All inputs are from stdin and outputs to stdout. Problem 2 Subset generation Level: Easy Given a set {1, 2, …, n}, 32 > n ≥ 1, generate all of its subsets. Sample test Input: Filename: 2.in Format: There are 10 lines to stand for 10 cases. Each line contains an integer number, n. Output: Filename: 2.out Format: 1. For each case, the first line is a prompt as following: “case 1:”. The next lines are all of the combinations. Notice that one line for one combination and list the results in following order. (1) Grouping conditions: i. The maximum element in each subset ii. The number of elements (2) All listing orders are ascending order. 2. For each combination, use two symbol, “{“ and “}”, to represent the start and termination respectively. To separate each element, please use the comma symbol(“,”). 3. You don’t have to print any thing if the result is null or overflow. 4. The last line of each case is an empty line, even if the result is null. Example: Input: 1 3 Output: case 1: {} {1} case 2: {} {1} {2} {1,2} {3} {1,3} {2,3} {1,2,3} Problem 3 Fibonacci numbers Level: Easy Given an unsigned integer n, compute the nth Finonacci number Fn according to the equation: n 0 1 Fn−1 Fn = , n ≥ 1 1 1 Fn Fn+1 Please put the result into a 4 bytes unsigned integer. If Fn overflows, your program shall report so. Sample test Input: Filename: 3.in Format: There are 10 lines to stand for 10 cases. Each line contains an integer number, n. Output: Filename: 3.out Format: 1. For each case, the first line is a prompt as following: “case 1:”. The next line is the answer. 2. Print a word, “overflow”, if the n is too large. 3. The last line of each case is an empty line, even if the result is overflow. Example: Input: 1 25 48 Output: case 1: 1 case 2: 75025 case 3: overflow All inputs are from stdin and outputs to stdout. Problem 4 Greatest common divisor Level: Easy Given unsigned integers a and b, find gcd(a,b), the greatest common divisor of a and b. ( 改成簡單的只求 gcd,則以下皆可省略。) , according to the definition: gcd(a, b) = a, if b ≤ 1 (Convention: gcd(0,0) = 0 ) gcd(a, b) = b, if a ≤ 1 More specific definition Case 1: a * b = 0 if a = 0 , gcd (a, b) = b if b = 0, gcd ( a, b) = a Case 2: a * b ≠ 0 if a = 1, gcd ( a, b) = 1 if b = 1, gcd ( a, b) = 1 gcd(a,b) = 2min(a2 ,b2 )3min(a3 ,b3 )5min(a5 ,b5 )7min(a7 ,b7 )11min(a11 ,b11 ) , if a,b ≥ 2 where a = 2a2 3a3 5a5 7a711a11 and b = 2b2 3b3 5b5 7b711b11 are prime factorizations of a and b, respectively. Sample test Input: Filename: 4.in Format: There are 10 lines to stand for 10 cases. Each line contains a set of a and b which separated with space. Output: Filename: 4.out Format: 1. For each case, the first line is a prompt as following: “case 1:”. The next line is the answer. 2. The last line of each case is an empty line. Example: Input: 0 0 0 1 2 0 4 6 Output: case 1: 0 case 2: 1 case 3: 2 case 4: 2 Problem 5 Integer partition Level: Easy Given an integer n ≥ 1, generate all the ways of summing up to n using integers 1, 2, …, n with repetition. Sample test Input: Filename: 5.in Format: There are 10 lines to stand for 10 cases. Each line contains an integer number, n. Output: Filename: 5.out Format: 1. For each case, the first line is a prompt as following: “case 1:”. The next lines are all of the results. Notice that one line for one result and list the results in descending order. 2. To separate each element, please use a space symbol. 3. For convenience, the termination of each result is space symbol. 4. You don’t have to print any thing if the result is null. 5. The last line of each case is an empty line, even if the result is null. Example: Input: 1 6 Output: case 1: 1 case 2: 6 5 1 4 2 4 1 1 3 3 3 2 1 3 1 1 1 2 2 2 2 2 1 1 2 1 1 1 1 1 1 1 1 1 1 Problem 6 Parser and evaluator Level: Hard Write a parser and an evaluator for the arithmetic expressions generated by the following grammar: E → T + E | T - E | T ⇐ addition/subtraction T → F * T | F / T | F % T | F F → ( E ) | - F | + F | N ⇐ plus/minus sign N → D | D N D → 0 | 1 | 2 | ... | 9 Observe that an expression may contain only integers (generated by N). Hence, the value of an expression is defined to be an integer. In particular, as in C and C++, / and % are integer division for quotient and remainder, respectively. The meanings of other operators are standard. The priority of operator: 1. – (Negative) 2. % 3. *, / 4. +, – (Minus) Sample test Input: Filename: 6.in Format: There are 10 lines to stand for 10 cases. Each line contains an expression. Output: Filename: 6.out Format: 1. For each case, the first line is a prompt as following: “case 1:”. The next line is the answer. 2. Print “syntactically incorrect”, if the input is not accepted. 3. The last line of each case is an empty line. Example: Input: 789-(400+300) 789-400+300 -9*80+72/61%7 72+((38-66) -101**29 123/78%23 45 Output: case 1: 89 case 2: 689 case 3: -706 case 4: syntactically incorrect case 5: syntactically incorrect case 6: syntactically incorrect All inputs are from stdin and outputs to stdout. Problem 7 – Binary tree generation Given an integer n 0, generate all binary trees with n nodes. The order of generation is right subtree first. Display the binary tree by rotating it 90∘counterclockwise. That is, display it in reverse inorder traversal (right subtree – root – left subtree). Input: Each line of input contains an integer n 0 specifying the number of nodes in the binary trees to be generated. End of input is indicated by -1. Output: Each binary tree generated should be in its own separate lines with no trailing spaces and should take minimum number of rows to display. Levels of the tree should be in consecutive columns. Asterisk, ‘*’, should be used to indicate tree nodes. No extra lines should be inserted between binary trees generated. After generating all the binary trees of one input line, print a separator ‘===’ Sample Input: 0 1 2 3 -1 Sample Output from Sample Input: === * === * * * * === * * * * * * * * * * * * * * * === All inputs are from stdin and outputs to stdout. Problem 8 – Prime factorization Given an integer n 2. Factor it into primes and use the prime factorization to determine the number of divisors of n and the sum of divisors of n. Let , where are primes and . The number of divisors of n can be calculated from . The sum of divisors of n can be calculated from . Input: Each line of input contains an integer n 0 specifying the number to be factored. End of input is indicated by -1. Output: For each test case, three output lines are generated. The first output line is the prime factorization. Each prime factor is represented by an ordered pair . The second and the third output lines are the number of divisors and the sum of divisors, respectively. At the end of each test case, please print a separator ‘===’. Sample Input: 12 14612 547114163 -1 Sample Output from Sample Input: (2,2)(3,1) 6 28 === (2,2)(13,1)(281,1) 12 27636 === (2333,1)(234511,1) 4 547351008 === All inputs are from stdin and outputs to stdout. Problem 9 – The next higher integer Given an integer n 0, find the next higher integer that can be formed by the digits of n. If n is already the maximum integer that can be formed by its digits, the next higher integer is defined to be n itself. Input: Each line of input contains an integer n 0.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages63 Page
-
File Size-