HDU 5308 I Wanna Become A 24-Point Master(2015多校第二场)

 
 
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 ith line contains one integer a,  one  char b and then one integer c,  where 
1ac<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
 
题意:有一个长度为2n-1的数组,数组中前n个数字都为n,对数组进行n-1次+-*/操作,将第i次的操作结果存入An+i中, 使得A2n-1等于24,数组中每个数只能使用一次, 中间运算可以有浮点数。
 
题解:对于1-13直接打表,14以上的数可以通过前12个相同数字得到24(转换成4*6),对于多余的n,我们可以用2个相减得0把多余的n乘掉。
 
注: 对于5而言,可以表示为5 * (5 - (5 / 5)/ 5)
 
  1 #include <cmath>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <iostream>
  5 #include <algorithm>
  6 using namespace std;
  7 int main()
  8 {
  9     int n;
 10     while (scanf("%d", &n) != EOF){
 11         if (n <= 3)
 12             printf("-1\n");
 13         else if (n == 4){
 14             printf("1 * 2\n");
 15             printf("5 + 3\n");
 16             printf("6 + 4\n");
 17         }
 18         else if (n == 5){
 19             printf("1 / 2\n");
 20             printf("6 / 3\n");
 21             printf("4 - 7\n");
 22             printf("5 * 8\n");
 23         }
 24         else if (n == 6){
 25             printf("1 + 2\n");
 26             printf("3 + 4\n");
 27             printf("5 - 6\n");
 28             printf("7 + 8\n");
 29             printf("9 + 10\n");
 30         }
 31         else if (n == 7){
 32             printf("1 + 2\n");
 33             printf("3 + 8\n");
 34             printf("9 / 4\n");
 35             printf("5 / 6\n");
 36             printf("11 + 7\n");
 37             printf("10 * 12\n");
 38         }
 39         else if (n == 8){
 40             printf("1 + 2\n");
 41             printf("3 + 9\n");
 42             printf("4 - 5\n");
 43             printf("11 * 6\n");
 44             printf("12 * 7\n");
 45             printf("13 * 8\n");
 46             printf("10 + 14\n");
 47         }
 48         else if (n == 9){
 49             printf("1 + 2\n");
 50             printf("3 + 4\n");
 51             printf("11 + 5\n");
 52             printf("12 + 6\n");
 53             printf("13 + 7\n");
 54             printf("14 + 8\n");
 55             printf("15 / 9\n");
 56             printf("10 + 16\n");
 57         }
 58         else if (n == 10){
 59             printf("1 + 2\n");
 60             printf("11 / 3\n");
 61             printf("4 + 12\n");
 62             printf("6 + 5\n");
 63             printf("14 + 7\n");
 64             printf("8 + 15\n");
 65             printf("9 + 10\n");
 66             printf("16 / 17\n");
 67             printf("13 * 18\n");
 68         }
 69         else if (n == 11){
 70             printf("1 + 2\n");
 71             printf("12 / 3\n");
 72             printf("4 / 5\n");
 73             printf("6 + 14\n");
 74             printf("7 - 8\n");
 75             printf("16 * 9\n");
 76             printf("17 * 10\n");
 77             printf("18 * 11\n");
 78             printf("13 * 15\n");
 79             printf("20 + 19\n");
 80         }
 81         else if (n == 12){
 82             printf("1 + 2\n");
 83             printf("3 - 4\n");
 84             printf("14 * 5\n");
 85             printf("6 * 15\n");
 86             printf("7 * 16\n");
 87             printf("8 * 17\n");
 88             printf("9 * 18\n");
 89             printf("10 * 19\n");
 90             printf("11 * 20\n");
 91             printf("12 * 21\n");
 92             printf("13 + 22\n");
 93         }
 94         else if (n == 13){
 95             printf("1 + 2\n");
 96             printf("3 + 14\n");
 97             printf("15 / 4\n");
 98             printf("5 + 6\n");
 99             for (int i=7; i<=12; i++)
100                 printf("%d + %d\n", i, i+10);
101             printf("23 / 13\n");
102             printf("16 * 24\n");
103         }
104         else{
105             printf("1 + 2\n");
106             for (int i=3; i<=4; i++)
107                 printf("%d + %d\n", i, n+i-2);
108             printf("%d / 5\n", n+3);
109             printf("6 + 7\n");
110             for (int i=8; i<=11; i++)
111                 printf("%d + %d\n", i, n+i-3);
112             printf("%d / 12\n", n+9);
113             printf("13 - 14\n");
114             for (int i=15; i<=n; i++)
115                 printf("%d * %d\n", i, n+i-4);
116             printf("%d * %d\n", n+4, n+10);
117             printf("%d + %d\n", 2*n-2, 2*n-3);
118         }
119     }
120     return 0;
121 }
View Code

 

转载于:https://www.cnblogs.com/hgfblog/p/4673758.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值