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

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  n100 . Each testcase contains only one integer  n (1n105)
 

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  2n1  numbers and at firsrt  Ai=n (1in) . You need to print  n1  lines and the  i th line contains one integer  a , one char  b  and then one integer c, where  1a,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.  A2n1=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 <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;
}


你可能感兴趣的:(Algorithm,编程,算法,ACM)