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≤ 1 0 5 10^5 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 A i A_i 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 A a A_a Aa and A c A_c Ac do the operation b and store the answer into A n + i A_{n+i} An+i.
If your answer satisfies the following rule, we think your answer is right:
- A 2 n − 1 A_{2n−1} A2n−1=24
- Each position of the array A is used at most one tine
- The absolute value of the numerator and denominator of each element in array A is no more than 1 0 9 10^9 109
Sample Input
4
Sample Output
1 * 2
5 + 3
6 + 4
题意:有一个长度为2n-1的数组,数组中前n个数字都为n,对数组进行n-1次±*/操作,将第i次的操作结果存入 A n + i A_{n+i} An+i中, 使得 A 2 n − 1 A_{2n-1} A2n−1等于24,数组中每个数只能使用一次, 中间运算可以有浮点数。
题解:对于1-13直接打表,14以上的数可以通过前12个相同数字得到24(转换成4*6),对于多余的n,我们可以用2个相减得0把多余的n乘掉。
注:对于5而言,可以表示为5 * (5 - (5 / 5)/ 5)
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int n;
while (scanf("%d", &n) != EOF){
if (n <= 3)
printf("-1\n");
else if (n == 4){
printf("1 * 2\n");
printf("5 + 3\n");
printf("6 + 4\n");
}
else if (n == 5){
printf("1 / 2\n");
printf("6 / 3\n");
printf("4 - 7\n");
printf("5 * 8\n");
}
else if (n == 6){
printf("1 + 2\n");
printf("3 + 4\n");
printf("5 - 6\n");
printf("7 + 8\n");
printf("9 + 10\n");
}
else if (n == 7){
printf("1 + 2\n");
printf("3 + 8\n");
printf("9 / 4\n");
printf("5 / 6\n");
printf("11 + 7\n");
printf("10 * 12\n");
}
else if (n == 8){
printf("1 + 2\n");
printf("3 + 9\n");
printf("4 - 5\n");
printf("11 * 6\n");
printf("12 * 7\n");
printf("13 * 8\n");
printf("10 + 14\n");
}
else if (n == 9){
printf("1 + 2\n");
printf("3 + 4\n");
printf("11 + 5\n");
printf("12 + 6\n");
printf("13 + 7\n");
printf("14 + 8\n");
printf("15 / 9\n");
printf("10 + 16\n");
}
else if (n == 10){
printf("1 + 2\n");
printf("11 / 3\n");
printf("4 + 12\n");
printf("6 + 5\n");
printf("14 + 7\n");
printf("8 + 15\n");
printf("9 + 10\n");
printf("16 / 17\n");
printf("13 * 18\n");
}
else if (n == 11){
printf("1 + 2\n");
printf("12 / 3\n");
printf("4 / 5\n");
printf("6 + 14\n");
printf("7 - 8\n");
printf("16 * 9\n");
printf("17 * 10\n");
printf("18 * 11\n");
printf("13 * 15\n");
printf("20 + 19\n");
}
else if (n == 12){
printf("1 + 2\n");
printf("3 - 4\n");
printf("14 * 5\n");
printf("6 * 15\n");
printf("7 * 16\n");
printf("8 * 17\n");
printf("9 * 18\n");
printf("10 * 19\n");
printf("11 * 20\n");
printf("12 * 21\n");
printf("13 + 22\n");
}
else if (n == 13){
printf("1 + 2\n");
printf("3 + 14\n");
printf("15 / 4\n");
printf("5 + 6\n");
for (int i=7; i<=12; i++)
printf("%d + %d\n", i, i+10);
printf("23 / 13\n");
printf("16 * 24\n");
}
else{
printf("1 + 2\n");
for (int i=3; i<=4; i++)
printf("%d + %d\n", i, n+i-2);
printf("%d / 5\n", n+3);
printf("6 + 7\n");
for (int i=8; i<=11; i++)
printf("%d + %d\n", i, n+i-3);
printf("%d / 12\n", n+9);
printf("13 - 14\n");
for (int i=15; i<=n; i++)
printf("%d * %d\n", i, n+i-4);
printf("%d * %d\n", n+4, n+10);
printf("%d + %d\n", 2*n-2, 2*n-3);
}
}
return 0;
}