hdu 5308 I Wanna Become A 24-Point Master(2015 Multi-University Training Contest 2)

5942 ワード

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
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 
b and then one integer c, where 
1≤a,cb 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,对这些数进行加减乘除操作,使其等于24。

解题思路:

     构造,当n大于12时,(n+n)/n*(n+n+n)/n*(n+n+n+n)/n为24, 接着就可以通过+n-n和+(n-n)/n操作来凑。其他的打表。

代码:
#include 
#include 
#include 
#include 
using namespace std;
int main()
{
    int n;
    //freopen("in.txt","r",stdin);
   // freopen("out.txt","w",stdout);
    while(~scanf("%d",&n))
    {
       //printf("%d
",n); if(n<=3) printf("-1
"); else if(n==4) { printf("1 * 2
5 + 3
6 + 4
"); } else if(n==5) { printf("1 * 2
6 * 3
7 - 4
8 / 5
"); } else if(n==6) { printf("1 + 2
7 + 3
8 + 4
9 + 5
10 - 6
"); } else if(n==7) { printf("1 + 2
8 + 3
4 + 5
10 + 6
11 / 7
9 + 12
"); } else if(n==8) { printf("1 + 2
9 + 3
4 + 5
11 - 6
12 - 7
13 / 8
10 + 14
"); } else if(n==9) { printf("1 + 2
10 + 3
4 + 5
12 + 6
13 / 7
11 - 14
15 - 8
16 + 9
"); } else if(n==10) { printf("1 + 2
3 + 4
12 + 5
13 + 6
14 / 7
11 + 15
8 - 9
17 / 10
16 + 18
"); } else if(n==11) { printf("1 + 2
3 + 4
13 / 5
12 + 14
15 - 6
16 + 7
17 - 8
18 + 9
19 - 10
20 + 11
"); } else if(n==13) { printf("1 + 2
3 + 4
15 / 5
14 - 16
17 - 6
18 + 7
19 - 8
20 + 9
21 - 10
22 + 11
23 - 12
24 + 13
"); } else { printf("1 + 2
%d / 3
4 + 5
%d + 6
%d / 7
8 + 9
%d + 10
%d + 11
%d / 12
%d * %d
%d * %d
",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
%d - %d
",i+n-2,i,i+n-1,i+1); } } else { printf("13 - 14
%d / 15
%d + %d
",n+12,n+11,n+13); for(int i=16;i<=n;i+=2) { printf("%d + %d
%d - %d
",i+n-2,i,i+n-1,i+1); } } } } return 0; }