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)


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

 凑24,对于n个数,我们需要用前7个数凑出6,用前5个数凑出4,然后相乘得到24,用第13、第14凑出0,用0与其余数相乘,最后24+0得到24。所以对于n小于14时,可以采用直接打表的方式或者DFS爆搜,大于14就可以按照套路直接得到24了。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define maxn 100010
#define ll long long
using namespace std;
int n;
char a[maxn];
bool dfs(int num, int dep)
{
        //cout<<num<<" "<<dep<<endl;
        if (dep == n - 1 && num == n)
        {
                return true;
        }
        if (dep == n - 1)
        {
                return false;
        }
        if (num - n >= 0)
        {
                if (dfs(num - n, dep + 1))
                {
                        a[dep] = '+';
                        return true;
                }
        }
        if (num + n <= 1e9)
        {
                if (dfs(num + n, dep + 1))
                {
                        a[dep] = '-';
                        return true;
                }
        }
        if (num % n == 0)
        {
                if (dfs(num / n, dep + 1))
                {
                        a[dep] = '*';
                        return true;
                }
        }
        if (num * n <= 1e9)
        {
                if (dfs(num * n, dep + 1))
                {
                        a[dep] = '/';
                        return true;
                }
        }
        return false;
}
int main()
{

        while (scanf("%d", &n) != EOF)
        {
                if (n <= 13)
                {
                        if (dfs(24, 0))
                        {
                                int k = 2;
                                for (int i = n - 2; i >= 0; i--)
                                {
                                        if (i == n - 2)
                                        {
                                                printf("%d %c %d\n", 1, a[i], k);
                                        }
                                        else
                                        {
                                                printf("%d %c %d\n", k + n - 2, a[i], ++k);
                                        }
                                }
                        }
                        else
                        {
                                printf("-1\n");
                        }
                }
                else
                {
                        puts("1 + 2");
                        printf("%d + 3\n", n + 1);
                        printf("%d + 4\n", n + 2);
                        printf("%d + 5\n", n + 3);
                        printf("%d + 6\n", n + 4);
                        printf("%d / 7\n", n + 5);
                        puts("8 + 9");
                        printf("%d + 10\n", n + 7);
                        printf("%d + 11\n", n + 8);
                        printf("%d / 12\n", n + 9);
                        printf("%d * %d\n", n + 6, n + 10);
                        puts("13 - 14");
                        int tep = n + 12;
                        for (int i = 15; i <= n; i++)
                        {
                                printf("%d * %d\n", i, tep);
                                tep++;
                        }
                        printf("%d + %d\n", n + 11, tep);
                }
        }
        return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值