Problem Description
Recently Rikka falls in love with an old but interesting game -- 24 points. She wants to become
a master of this game, so she asks Yuta to give her some problems to practice.
Quickly, Rikka solved almost all of the problems but the remained one is really difficult:
In this problem, you need to write a program which can get 24 points with n numbers, which are all equal to n.
Input
There are no more then 100 testcases and there are no more then 5 testcases with n≥100. Each testcase contains only one integer n (1≤n≤105)
Output
For each testcase:
If there is not any way to get 24 points, print a single line with -1.
Otherwise, let A be an array with 2n−1 numbers and at firsrt Ai=n (1≤i≤n). You need to print
If there is not any way to get 24 points, print a single line with -1.
Otherwise, let A be an array with 2n−1 numbers and at firsrt Ai=n (1≤i≤n). You need to print
n−1 lines and the ith line contains one integer a, one char b and then one integer c, where
1≤a, c<n+i and b is "+","-","*" or "/". This line means that you let Aa and Ac do the operation
b and store the answer into An+i.
If your answer satisfies the following rule, we think your answer is right:
1. A2n−1=24
2. Each position of the array A is used at most one tine.
3. The absolute value of the numerator and denominator of each element in array A is no more
than 109
Sample Input
4
Sample Output
1 * 2
5 + 3
6 + 4
题意:有一个长度为2n-1的数组,数组中前n个数字都为n,对数组进行n-1次+-*/操作,将第i次的操作结果存入An+i中, 使得A2n-1等于24,数组中每个数只能使用一次, 中间运算可以有浮点数。
题解:对于1-13直接打表,14以上的数可以通过前12个相同数字得到24(转换成4*6),对于多余的n,我们可以用2个相减得0把多余的n乘掉。
注:对于5而言,可以表示为5 * (5 - (5 / 5)/ 5)
1 #include <cmath> 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 int main() 8 { 9 int n; 10 while (scanf("%d", &n) != EOF){ 11 if (n <= 3) 12 printf("-1 "); 13 else if (n == 4){ 14 printf("1 * 2 "); 15 printf("5 + 3 "); 16 printf("6 + 4 "); 17 } 18 else if (n == 5){ 19 printf("1 / 2 "); 20 printf("6 / 3 "); 21 printf("4 - 7 "); 22 printf("5 * 8 "); 23 } 24 else if (n == 6){ 25 printf("1 + 2 "); 26 printf("3 + 4 "); 27 printf("5 - 6 "); 28 printf("7 + 8 "); 29 printf("9 + 10 "); 30 } 31 else if (n == 7){ 32 printf("1 + 2 "); 33 printf("3 + 8 "); 34 printf("9 / 4 "); 35 printf("5 / 6 "); 36 printf("11 + 7 "); 37 printf("10 * 12 "); 38 } 39 else if (n == 8){ 40 printf("1 + 2 "); 41 printf("3 + 9 "); 42 printf("4 - 5 "); 43 printf("11 * 6 "); 44 printf("12 * 7 "); 45 printf("13 * 8 "); 46 printf("10 + 14 "); 47 } 48 else if (n == 9){ 49 printf("1 + 2 "); 50 printf("3 + 4 "); 51 printf("11 + 5 "); 52 printf("12 + 6 "); 53 printf("13 + 7 "); 54 printf("14 + 8 "); 55 printf("15 / 9 "); 56 printf("10 + 16 "); 57 } 58 else if (n == 10){ 59 printf("1 + 2 "); 60 printf("11 / 3 "); 61 printf("4 + 12 "); 62 printf("6 + 5 "); 63 printf("14 + 7 "); 64 printf("8 + 15 "); 65 printf("9 + 10 "); 66 printf("16 / 17 "); 67 printf("13 * 18 "); 68 } 69 else if (n == 11){ 70 printf("1 + 2 "); 71 printf("12 / 3 "); 72 printf("4 / 5 "); 73 printf("6 + 14 "); 74 printf("7 - 8 "); 75 printf("16 * 9 "); 76 printf("17 * 10 "); 77 printf("18 * 11 "); 78 printf("13 * 15 "); 79 printf("20 + 19 "); 80 } 81 else if (n == 12){ 82 printf("1 + 2 "); 83 printf("3 - 4 "); 84 printf("14 * 5 "); 85 printf("6 * 15 "); 86 printf("7 * 16 "); 87 printf("8 * 17 "); 88 printf("9 * 18 "); 89 printf("10 * 19 "); 90 printf("11 * 20 "); 91 printf("12 * 21 "); 92 printf("13 + 22 "); 93 } 94 else if (n == 13){ 95 printf("1 + 2 "); 96 printf("3 + 14 "); 97 printf("15 / 4 "); 98 printf("5 + 6 "); 99 for (int i=7; i<=12; i++) 100 printf("%d + %d ", i, i+10); 101 printf("23 / 13 "); 102 printf("16 * 24 "); 103 } 104 else{ 105 printf("1 + 2 "); 106 for (int i=3; i<=4; i++) 107 printf("%d + %d ", i, n+i-2); 108 printf("%d / 5 ", n+3); 109 printf("6 + 7 "); 110 for (int i=8; i<=11; i++) 111 printf("%d + %d ", i, n+i-3); 112 printf("%d / 12 ", n+9); 113 printf("13 - 14 "); 114 for (int i=15; i<=n; i++) 115 printf("%d * %d ", i, n+i-4); 116 printf("%d * %d ", n+4, n+10); 117 printf("%d + %d ", 2*n-2, 2*n-3); 118 } 119 } 120 return 0; 121 }