I Wanna Become A 24-Point Master
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 481 Accepted Submission(s): 190
Special Judge
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.
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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
int n;
//freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
while(~scanf("%d",&n))
{
//printf("%d\n",n);
if(n<=3)
printf("-1\n");
else if(n==4)
{
printf("1 * 2\n5 + 3\n6 + 4\n");
}
else if(n==5)
{
printf("1 * 2\n6 * 3\n7 - 4\n8 / 5\n");
}
else if(n==6)
{
printf("1 + 2\n7 + 3\n8 + 4\n9 + 5\n10 - 6\n");
}
else if(n==7)
{
printf("1 + 2\n8 + 3\n4 + 5\n10 + 6\n11 / 7\n9 + 12\n");
}
else if(n==8)
{
printf("1 + 2\n9 + 3\n4 + 5\n11 - 6\n12 - 7\n13 / 8\n10 + 14\n");
}
else if(n==9)
{
printf("1 + 2\n10 + 3\n4 + 5\n12 + 6\n13 / 7\n11 - 14\n15 - 8\n16 + 9\n");
}
else if(n==10)
{
printf("1 + 2\n3 + 4\n12 + 5\n13 + 6\n14 / 7\n11 + 15\n8 - 9\n17 / 10\n16 + 18\n");
}
else if(n==11)
{
printf("1 + 2\n3 + 4\n13 / 5\n12 + 14\n15 - 6\n16 + 7\n17 - 8\n18 + 9\n19 - 10\n20 + 11\n");
}
else if(n==13)
{
printf("1 + 2\n3 + 4\n15 / 5\n14 - 16\n17 - 6\n18 + 7\n19 - 8\n20 + 9\n21 - 10\n22 + 11\n23 - 12\n24 + 13\n");
}
else
{
printf("1 + 2\n%d / 3\n4 + 5\n%d + 6\n%d / 7\n8 + 9\n%d + 10\n%d + 11\n%d / 12\n%d * %d\n%d * %d\n",n+1,n+3,n+4,n+6,n+7,n+8,n+2,n+5,n+9,n+10);
if(n%2==0)
{
for(int i=13;i<=n;i+=2)
{
printf("%d + %d\n%d - %d\n",i+n-2,i,i+n-1,i+1);
}
}
else
{
printf("13 - 14\n%d / 15\n%d + %d\n",n+12,n+11,n+13);
for(int i=16;i<=n;i+=2)
{
printf("%d + %d\n%d - %d\n",i+n-2,i,i+n-1,i+1);
}
}
}
}
return 0;
}