码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 5308 I Wanna Become A 24-Point Master

时间:2015-07-23 23:48:20      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

I Wanna Become A 24-Point Master

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 366    Accepted Submission(s): 141
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 2n?1 numbers and at firsrt Ai=n (1in). You need to print n?1 lines and the ith line contains one integer a, one char band 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. 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
 

Source
 


#include <bits/stdc++.h>
using namespace std;
#define prt(k) cerr<<#k" = "<<k<<endl
typedef long long ll;
int n;
const int N = 200222;
int d[N];
int t;
bool vis[N];
int a, b;
inline void g(int x, char c, int y)
{
        assert(!vis[x] && !vis[y] && 1<=x&&x<=t && 1<=y&&y<=t);
        vis[x] = vis[y] = 1;
        if (c=='+') d[++t] = d[x] + d[y];
        if (c=='-') d[++t] = d[x] - d[y];
        if (c=='*') d[++t] = d[x] * d[y];
        if (c=='/') d[++t] = d[x] / d[y];
        if (t==2*n-1) assert(d[t]==24);
}
inline void f(int x, char c, int y)
{
        printf("%d %c %d\n", x, c, y);
        g(x, c, y);
}
inline void f(char c)
{
        if (a==1 ) { f(1, c, 2); a=3; return; }
        f(b++, c, a++);
}
inline void f(string s)
{
        int x, y; char op[5], c;
        sscanf(s.c_str(), "%d%s%d", &x, op, &y);
        c = op[0];
        puts(s.c_str());
        g(x, c, y);
}

int main()
{
        while (scanf("%d", &n)==1 )
        {
                memset(vis, 0 , sizeof vis);
                if (n < 4) {
                        puts("-1");
                        continue;
                }
                t = n;
                memset(d, -1, sizeof d);
                for (int i=1;i<=n;i++) d[i]=n;
                a = 1, b=n+1;
                if (n == 4) {
                        f(1, '*', 2);
                        f(5, '+', 3);
                        f(6, '+', 4);
                        continue;
                }
                if (n == 5) { /// (5*5*5-5)/5
                        f(1, '*', 2); /// a6 = 25
                        f(6, '*', 3); /// a7 = 1
                        f(7, '-', 4); ///a8 = 24
                        f(8, '/', 5);
                       // continue;
                }
                if (n==6) {
                        f("1 * 2");
                        f("7 * 3");
                        f("8 / 4");
                        f("9 - 5");
                        f("10 - 6");
                }
                if (n==7) {
                        f("1 + 2");
                        f("3 + 8");
                        f("9 / 4");
                        f("10 + 5");
                        f("11 + 6");
                        f("12 + 7");
                }
                if (n==8) {
                        f("1 + 2");
                        f("3 + 9");
                        f("4 - 5");
                        f("11 * 6");
                        f("12 * 7");
                        f("13 * 8");
                        f("14 + 10");
                }
                if (n==9) {
                        f('+');
                        f('+');
                        int p=  b++;
                        f(a++,'/',a++);
                        f(p, '-', b++);
                        p = b++;
                        f(a++,'/',a++); /// get 1;
                        f(p, '-', b++);
                        p = b++;
                        f(a++,'/',a++); /// get 1;
                        f(p, '-', b++);
                }
                if (n == 12 || n>=15) {
                        f('+');
                        f(b++, '/', a++); int p2 = b;
                        b++;
                        f(a++, '+', a++);
                        f('+');
                        f(b++, '/', a++);
                        f(p2,'*',b++); int p6 = b;
                        b++;
                        f(a++, '+', a++);
                        f('+');
                        f('+');
                        f(b++, '/', a++);
                        f(p6, '*', b++);
                        if (n==12) continue;
                        int p24 = b++;
                        f(a++,'-',a++);
                        for (int i=0;i<n-14;i++) {
                                f('*');
                        }
                        f(b++,'+',p24);
                }
                if (10<=n && n<=11) {
                        int p;
                        f('+');
                        p = b++;
                        for (int i=0;i<24-2*n;i++)
                        {
                                f(a++,'/',a++);
                                f(p, '+', b++);
                                p = b++;
                        }
                        if (n==11) {
                                f(a++,'-',a++);
                                for (int i=0;i<3;i++)
                                {
                                        f('*');
                                }
                                f(p, '+', b);
                        }
                }
                if (13<=n && n<=14) {
                        int p;
                        f('+');
                        p = b++;
                        for (int i=0;i<2*n-24;i++)
                        {
                                f(a++,'/',a++);
                                f(p, '-', b++);
                                p = b++;
                        }

                        f(a++,'-',a++);
                        for (int i=0;i<(n==13 ? 5 : 2);i++)
                        {
                                f('*');
                        }
                        f(p, '+', b);
                }
                assert(d[2*n-1]==24);
        }
        return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

HDU 5308 I Wanna Become A 24-Point Master

标签:

原文地址:http://blog.csdn.net/oilover/article/details/47029791

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!