HDU 5308 I Wanna Become A 24-Point Master
来源:互联网 发布:已知矩阵的秩求未知数 编辑:程序博客网 时间:2024/06/06 16:41
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 withn numbers, which are all equal to n .
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
Input
There are no more then 100 testcases and there are no more then 5 testcases with n≥100 . Each testcase contains only one integer n (1≤n≤105)
Output
For each testcase:
If there is not any way to get 24 points, print a single line with -1.
Otherwise, letA be an array with 2n−1 numbers and at firsrt Ai=n (1≤i≤n) . You need to print n−1 lines and the i th line contains one integer a , one char b and then one integer c, where 1≤a,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 arrayA is used at most one tine.
3. The absolute value of the numerator and denominator of each element in arrayA is no more than 109
If there is not any way to get 24 points, print a single line with -1.
Otherwise, let
If your answer satisfies the following rule, we think your answer is right:
1.
2. Each position of the array
3. The absolute value of the numerator and denominator of each element in array
Sample Input
4
Sample Output
1 * 25 + 36 + 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 longusing 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;}
0 0
- HDU 5308 I Wanna Become A 24-Point Master
- hdu 5308 I Wanna Become A 24-Point Master
- HDU 5308 I Wanna Become A 24-Point Master
- HDOJ 5308 I Wanna Become A 24-Point Master 构造
- [多校2015.02.1009 构造] hdu 5308 I Wanna Become A 24-Point Master
- hdu 5308 I Wanna Become A 24-Point Master 2015 Multi-University Training Contest 2
- hdu 5308 I Wanna Become A 24-Point Master(2015 Multi-University Training Contest 2)
- HDU 5308 I Wanna Become A 24-Point Master(找规律+模拟)
- HDU 5308 I Wanna Become A 24-Point Master(2015多校第二场)
- hdu 5301 Buildings 5308 I Wanna Become A 24-Point Master(构造)
- HDU 5308 I Wanna Become A 24-Point Master(模拟+数论)
- hdu 5308 I Wanna Become A 24-Point Master(构造)
- hdu 5308 I Wanna Become A 24-Point Master(构造)
- HDU 5308 I Wanna Become A 24-Point Master (暴力枚举+构造)
- HDU 5308 I Wanna Become A 24-Point Master 循环(散乱的前缀+循环体)
- 找规律,模拟(HDU 5308,I Wanna Become A 24-Point Master)
- hdu5308 I Wanna Become A 24-Point Master 含 spj
- hdu5308 I Wanna Become A 24-Point Master(构造)
- 判断质数因子
- 【FCC】Build a Tribute Page(html+css+bootstrap)
- 数据化运营
- Android四大组件之Activity,Activity生命周期
- PAT1006
- HDU 5308 I Wanna Become A 24-Point Master
- QTP 正则表达式 学习
- 【HDU1512】Monkey King-左偏树+并查集(左偏树入门题)
- 【LeetCode】485. Max Consecutive Ones
- A Java RunTime Environment (JRE) or Java Development Kit (JDK) must be available in order
- topology中的配置文件和配置类
- java基础-------永远分不清的位运算符 与(&)、非(~)、或(|)、异或(^)
- Eclipse常用快捷键
- Java学习第十五天之Collection、Iterator、List