2015 Multi-University Training Contest 2 1009 I Wanna Become A 24-Point Master(HDU5308) 构造

I Wanna Become A 24-Point Master

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

Recommend
wange2014   |   We have carefully selected several similar problems for you:   5309  5307  5306  5304  5303 
 

题意:

1个1,2个2,3个3,4个4,5个5…… 100000个100000用加减乘除括号算24。

输出的格式是,一个数组有2 * n - 1个数,前n个数都为n,则输出n - 1个式子,含义是下标为x的与下标y的做XX操作,结果会被保存在下一个未保存数字的单元内,要让第2 * n - 1个数为24

思路:

用12个n可以用如下的方法算24:

(n + n + n + n) / n * (n + n + n  + n + n  + n) / n = 24

跟n的值无关,所以只要不小于12的偶数都可以用此方法,先用前12个算出24,然后+n - n 就行了

同理,13个n可以用如下的方法算24,这样可以解决不小于13的奇数:

(n + n + n ) / n * (n + n + n  + n + n  + n  + n  + n) / n = 24

这样12及以后的就都搞定了,12之前的手算一下就可以打表了。


#include <cstdio>

using namespace std;
///1——11的表
char str[12][150]= {"",
                    "-1",
                    "-1",
                    "-1",
                    "1 * 2\n5 + 3\n6 + 4",
                    "1 * 2\n3 * 6\n7 - 4\n8 / 5",
                    "1 + 2\n3 + 7\n4 + 8\n5 + 9\n10 - 6",
                    "1 + 2\n3 + 8\n4 + 5\n6 + 10\n11 / 7\n9 + 12",
                    "1 + 2\n3 + 9\n4 - 5\n11 * 6\n12 * 7\n13 * 8\n14 + 10",
                    "1 + 2\n10 + 3\n11 / 4\n5 * 12\n6 + 7\n14 + 8\n15 / 9\n13 - 16",
                    "1 + 2\n3 + 11\n4 + 5\n13 + 6\n14 + 7\n15 + 8\n16 + 9\n17 / 10\n12 - 18",
                    "1 + 2\n3 + 4\n13 / 5\n12 + 14\n6 - 7\n8 - 9\n10 - 11\n15 + 16\n18 + 17\n19 + 20",
                   };

int main() {
    int n;
    while(~scanf("%d",&n)) {
		///1——11的按表输出
        if(n < 12) {
            puts(str[n]);
		///超过11,经行构造,前面一截是一样的
        } else {
            printf("1 + 2\n3 + 4\n5 + 6\n7 + 8\n9 + 10\n");
			///偶数的构造方法 (4*6)
            if(n % 2 == 0) {
                printf("%d + %d\n",n + 1,n + 2);
                printf("%d + %d\n",n + 3,n + 4);
                printf("%d + %d\n",n + 5,n + 7);
                printf("%d / %d\n",n + 6,11);
                printf("%d / %d\n",n + 8,12);
                printf("%d * %d\n",n + 9,n + 10);
				///然后补+n-n
                for(int i = 13; i < n; i += 2) {
                    printf("%d + %d\n%d - %d\n",n + i - 2,i,n + i - 1,i + 1);
                }
			///奇数的构造方法(3*8)
            } else {
                printf("%d + %d\n",n + 1,11);
                printf("%d + %d\n",n + 2,n + 3);
                printf("%d + %d\n",n + 4,n + 5);
                printf("%d + %d\n",n + 7,n + 8);
                printf("%d / %d\n",n + 6,12);
                printf("%d / %d\n",n + 9,13);
                printf("%d * %d\n",n + 10,n + 11);
				///然后补+n-n
                for(int i = 14;i < n;i += 2){
                    printf("%d + %d\n%d - %d\n",n + i - 2,i,n + i - 1,i + 1);
                }
            }
        }
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值