HDU 5308 I Wanna Become A 24-Point Master(打表+找规律)

I Wanna Become A 24-Point Master

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1679    Accepted Submission(s): 712
Special Judge

 

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 n−1 lines and the ith line contains one integer a, one char band 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

题意:给你n个n,要你用n-1步算出24,而且正好是第n-1步结果为24,每个数只能用一次(包括n-1步里前n-2步的数,让你输出数组下标(n-1步从数组下标n+1开始,到a[2*n-1]))

思路:打表+推规律。题意读了半天。。。关键是只要推出来14项以后可以直接用12项拼4*6,然后14项-13项为0乘以多余的项,最后加起来就行了。至于前13项,当然是手撕。。。不过一定要注意别推错了,我刚开始第10项推错了导致WA了两发,改过来就直接过了。(比赛的时候这种题基本做不出来,因为时间不够,时间够的话还是能做出来的)

代码:

#include<bits/stdc++.h>
#define ll long long
#define maxn 410
#define inf 0x3f3f3f3f
#define lson i<<1,l,mid
#define rson i<<1|1,mid+1,r
using namespace std;
char s[maxn];
int n,m,flag,k;
void go4(){printf("1 * 2\n5 + 3\n6 + 4\n");}
void go5(){printf("1 * 2\n6 * 3\n7 - 4\n8 / 5\n");}
void go6(){printf("1 + 2\n7 + 3\n8 + 4\n9 + 5\n10 - 6\n");}
void go7(){printf("1 * 2\n3 / 4\n8 - 9\n5 + 6\n11 / 7\n10 / 12\n");}
void go8(){printf("1 + 2\n3 + 9\n4 - 5\n11 * 6\n12 * 7\n13 * 8\n10 + 14\n");}
void go9(){printf("1 + 2\n10 + 3\n4 / 5\n11 * 12\n6 + 7\n14 + 8\n15 / 9\n13 - 16\n");}
void go10(){printf("1 * 2\n11 + 3\n12 + 4\n5 + 6\n14 + 7\n15 + 8\n16 + 9\n17 / 10\n13 / 18\n");}
void go11(){printf("1 + 2\n3 + 4\n13 / 5\n6 - 7\n15 * 8\n16 * 9\n17 * 10\n18 * 11\n19 + 12\n20 + 14\n");}
void go12(){printf("1 + 2\n13 + 3\n14 - 4\n15 + 5\n16 - 6\n17 + 7\n18 - 8\n19 + 9\n20 - 10\n21 + 11\n22 - 12\n");}
void go13(){printf("1 + 2\n3 + 4\n15 / 5\n6 - 7\n17 * 8\n18 * 9\n19 * 10\n20 * 11\n21 * 12\n22 * 13\n14 + 23\n24 - 16\n");}
void go14(){
m=n+1;
printf("1 + 2\n");
printf("%d + 3\n",m++);
printf("%d + 4\n",m++);
printf("%d + 5\n",m++);
printf("%d + 6\n",m++);
printf("%d / 7\n",m++);
printf("8 + 9\n");m++;
printf("%d + 10\n",m++);
printf("%d + 11\n",m++);
printf("%d / 12\n",m++);
printf("%d * %d\n",n+6,m++);k=m;
printf("13 - 14\n");m++;//12bu
int f=15;
while(n>14){printf("%d * %d\n",m++,f++);n--;}
printf("%d + %d\n",k,m);
}
int main()
{
    int T,cas=1;
    while(scanf("%d",&n)!=EOF)
    {
        if(n<4){puts("-1");continue;}
        else if(n==4) go4();
        else if(n==5) go5();
        else if(n==6) go6();
        else if(n==7) go7();
        else if(n==8) go8();
        else if(n==9) go9();
        else if(n==10) go10();
        else if(n==11) go11();
        else if(n==12) go12();
        else if(n==13) go13();
        else go14();
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/lsd20164388/article/details/81083091