HDOJ 5308 I Wanna Become A 24-Point Master 构造


构造题,前面十几个手工处理....

n很大时有很多构造方法,一阵乱搞就可以了......


I Wanna Become A 24-Point Master

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 128    Accepted Submission(s): 36
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
 

Source
 



#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdlib>
using namespace std;

int dy[55];

void db(int x )
{
    if ( x==1)
        printf("-1\n");
    if ( x==2)
        printf("-1\n");
    if ( x==3)
        printf("-1\n");
    if ( x==4)
    {
        puts("1 * 2");
        puts("5 + 3");
        puts("6 + 4");
    }
    if ( x==5)
    {
        puts("1 / 2");
        puts("6 / 3");
        puts("4 - 7");
        puts("5 * 8");
    }
    if ( x==6)
    {
        puts("1 + 2");
        puts("7 + 3");
        puts("8 + 4");
        puts("9 * 5");
        puts("10 / 6");
    }
    if ( x==7)
    {
        puts("1 * 2");   //   8
        puts("3 / 4");   //   9
        puts("8 - 9");   //   10
        puts("5 + 6");   //   11
        puts("11 / 7");  //   12
        puts("10 / 12");   // 13
    }
    if ( x==8)
    {
        puts("1 + 2");   //   9
        puts("3 + 9");   //   10
        puts("4 - 5");   //   11
        puts("11 * 6");  //  12
        puts("12 * 7");   // 13
        puts("13 * 8");   // 14
        puts("10 + 14");   //  15
    }
    if ( x==9 )
    {
        puts("1 + 2");   // 10
        puts("10 + 3");
        puts("11 + 4");
        puts("12 + 5");
        puts("13 + 6");  // 14
        puts("14 / 7");  // 15
        puts("15 + 8");
        puts("16 + 9");
    }
    if ( x== 10)
    {
        puts("1 / 2");  //11
        puts("3 / 4");  //12
        puts("5 / 6");  // 13
        puts("7 / 8");  // 14
        puts("9 + 10");  //15
        puts("15 + 11");
        puts("16 + 12");
        puts("17 + 13");
        puts("18 + 14");
    }
    if ( x== 11)
    {
        puts("1 + 2");   //12
        puts("12 / 3");   // 13
        puts("13 * 4");  // 14
        puts("14 / 5");  //15
        puts("15 * 6"); //16
        puts("16 / 7"); //17
        puts("17 * 8");  // 18
        puts("18 / 9");  //19
        puts("19 + 10");  //20
        puts("20 + 11");   // 21
    }
    if ( x== 12)
    {
        puts("1 + 2");   // 13
        puts("13 + 3");   // 14
        puts("14 - 4");
        puts("15 + 5");
        puts("16 - 6"); 
        puts("17 + 7");
        puts("18 - 8");
        puts("19 + 9");
        puts("20 - 10");
        puts("21 + 11");
        puts("22 - 12");
    }

    if (x == 24)
    {
        puts("1 - 2");
        puts("25 * 3");
        puts("26 * 4");
        puts("27 * 5");
        puts("28 * 6");
        puts("29 * 7");
        puts("30 * 8");
        puts("31 * 9");
        puts("32 * 10");
        puts("33 * 11");
        puts("34 * 12");
        puts("35 * 13");
        puts("36 * 14");
        puts("37 * 15");
        puts("38 * 16");
        puts("39 * 17");
        puts("40 * 18");
        puts("41 * 19");
        puts("42 * 20");
        puts("43 * 21");
        puts("44 * 22");
        puts("45 * 23");
        puts("46 + 24");
    }
    if (x == 26)
    {
        puts("1 / 2");   //  27
        puts("3 / 4");   // 28
        puts("27 + 28");   //  29
        puts("5 - 29");   // 30
        puts("30 * 6");
        puts("31 / 7");
        puts("32 * 8");
        puts("33 / 9");
        puts("34 * 10");
        puts("35 / 11");
        puts("36 * 12");
        puts("37 / 13");
        puts("38 * 14");
        puts("39 / 15");
        puts("40 * 16");
        puts("41 / 17");
        puts("42 * 18");
        puts("43 / 19");
        puts("44 * 20");
        puts("45 / 21");
        puts("46 * 22");
        puts("47 / 23");
        puts("24 - 25");
        puts("49 * 26");
        puts("48 + 50");

    }
}

void solve25()
{
    int n = 25;
    int t = 1, e = dy[n + 1];
    printf("%d + %d\n", dy[t], dy[t + 1]);
    t += 2;
    for(int i = 0; i < 22; i++)
    {
        printf("%d + %d\n", dy[t++], e++);
    }
    printf("%d / %d\n", e++, dy[t++]);
}

void xiao(int n, int c)
{
    int t = 1, e = n + 1;
    printf("%d - %d\n", t, t + 1);
    t += 2;
    for(int i = 2; i < c; i++)
    {
        printf("%d * %d\n", t++, e++);
    }
    printf("%d + %d\n", e++, t++);
    int i;
    //printf("n - c = %d\n", n - c);
    for(i = 1; i < n - c; i++)
    {
        dy[i] = t++;
    }
    for(; i <= 55; i++)
    {
        dy[i] = e++;
    }
    //printf("*****%d\n", dy[26]);
    //for(int i = 1; i <= 26; i++) printf("%d ", dy[i]);printf("\n");
}


void solve24(int n)
{
    if(n == 26)
    {
        return;
    }
    for(int i = 1; i <= 88; i++) dy[i] = i;
    if(n >= 27)
    {
        xiao(n, n - 25);
    }
    solve25();
}



void solve13(int n)
{
    for(int i = 1; i < 55; i++) dy[i] = i;
    if(n > 13)
    {
        xiao(n, 2 * n - 26);
    }
    int m = 26 - n;
    int t = 1, e = dy[m + 1];
    if(m > 3)
    {
        printf("%d + %d\n", dy[t], dy[t + 1]);
        t += 2;
    }
    else
    {
        e--;
    }
    for(int i = 0; i < m - 4; i++)
    {
        printf("%d + %d\n", dy[t++], e++);
    }
    printf("%d / %d\n", e++, dy[t++]);
    printf("%d + %d\n", dy[t++], e++);
}

void solve(int n)
{
    if(n>=1&&n<=12) db(n);
    else if(n==24||n==26) db(n);
    else if(n > 24)
    {
        solve24(n);
    }
    else if(n >= 13)
    {
        solve13(n);
    }
}

int main()
{
    int n;
    while(scanf("%d", &n) != EOF)
    {
        solve(n);
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值