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)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
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\n"); 13 else if (n == 4){ 14 printf("1 * 2\n"); 15 printf("5 + 3\n"); 16 printf("6 + 4\n"); 17 } 18 else if (n == 5){ 19 printf("1 / 2\n"); 20 printf("6 / 3\n"); 21 printf("4 - 7\n"); 22 printf("5 * 8\n"); 23 } 24 else if (n == 6){ 25 printf("1 + 2\n"); 26 printf("3 + 4\n"); 27 printf("5 - 6\n"); 28 printf("7 + 8\n"); 29 printf("9 + 10\n"); 30 } 31 else if (n == 7){ 32 printf("1 + 2\n"); 33 printf("3 + 8\n"); 34 printf("9 / 4\n"); 35 printf("5 / 6\n"); 36 printf("11 + 7\n"); 37 printf("10 * 12\n"); 38 } 39 else if (n == 8){ 40 printf("1 + 2\n"); 41 printf("3 + 9\n"); 42 printf("4 - 5\n"); 43 printf("11 * 6\n"); 44 printf("12 * 7\n"); 45 printf("13 * 8\n"); 46 printf("10 + 14\n"); 47 } 48 else if (n == 9){ 49 printf("1 + 2\n"); 50 printf("3 + 4\n"); 51 printf("11 + 5\n"); 52 printf("12 + 6\n"); 53 printf("13 + 7\n"); 54 printf("14 + 8\n"); 55 printf("15 / 9\n"); 56 printf("10 + 16\n"); 57 } 58 else if (n == 10){ 59 printf("1 + 2\n"); 60 printf("11 / 3\n"); 61 printf("4 + 12\n"); 62 printf("6 + 5\n"); 63 printf("14 + 7\n"); 64 printf("8 + 15\n"); 65 printf("9 + 10\n"); 66 printf("16 / 17\n"); 67 printf("13 * 18\n"); 68 } 69 else if (n == 11){ 70 printf("1 + 2\n"); 71 printf("12 / 3\n"); 72 printf("4 / 5\n"); 73 printf("6 + 14\n"); 74 printf("7 - 8\n"); 75 printf("16 * 9\n"); 76 printf("17 * 10\n"); 77 printf("18 * 11\n"); 78 printf("13 * 15\n"); 79 printf("20 + 19\n"); 80 } 81 else if (n == 12){ 82 printf("1 + 2\n"); 83 printf("3 - 4\n"); 84 printf("14 * 5\n"); 85 printf("6 * 15\n"); 86 printf("7 * 16\n"); 87 printf("8 * 17\n"); 88 printf("9 * 18\n"); 89 printf("10 * 19\n"); 90 printf("11 * 20\n"); 91 printf("12 * 21\n"); 92 printf("13 + 22\n"); 93 } 94 else if (n == 13){ 95 printf("1 + 2\n"); 96 printf("3 + 14\n"); 97 printf("15 / 4\n"); 98 printf("5 + 6\n"); 99 for (int i=7; i<=12; i++) 100 printf("%d + %d\n", i, i+10); 101 printf("23 / 13\n"); 102 printf("16 * 24\n"); 103 } 104 else{ 105 printf("1 + 2\n"); 106 for (int i=3; i<=4; i++) 107 printf("%d + %d\n", i, n+i-2); 108 printf("%d / 5\n", n+3); 109 printf("6 + 7\n"); 110 for (int i=8; i<=11; i++) 111 printf("%d + %d\n", i, n+i-3); 112 printf("%d / 12\n", n+9); 113 printf("13 - 14\n"); 114 for (int i=15; i<=n; i++) 115 printf("%d * %d\n", i, n+i-4); 116 printf("%d * %d\n", n+4, n+10); 117 printf("%d + %d\n", 2*n-2, 2*n-3); 118 } 119 } 120 return 0; 121 }