Codeforces Round#268(Div 2)C 24 Game
2014-09-22 09:08
302 查看
题目链接:【戳这里】
题目大意:初始你手中有n张牌,数字分别是1~n,然后你任选其中两张牌,用“+”,“-”,“*”,来组合出另一张牌(另一个数字),并把使用过的牌扔掉,问最后能否剩下的最后一张牌为24?如果不能,输出NO;否则,输出YES,并将如何得到24的方法打印出来。
解题思路:由于此题是special judge,答案不唯一,所以很容易想到要去找某个循环节。那么我们可以简单的动笔试一下,n < 4时,最大也凑不到24;n = 4时,1*2*3*4 = 24;n = 5时,3 * 5 + 2 * 4 + 1 = 24;n >= 6时,由于存在减法和乘法,于是我们只要先凑出24,然后将24重复乘以[n - (n - 1)]即可。
题目大意:初始你手中有n张牌,数字分别是1~n,然后你任选其中两张牌,用“+”,“-”,“*”,来组合出另一张牌(另一个数字),并把使用过的牌扔掉,问最后能否剩下的最后一张牌为24?如果不能,输出NO;否则,输出YES,并将如何得到24的方法打印出来。
解题思路:由于此题是special judge,答案不唯一,所以很容易想到要去找某个循环节。那么我们可以简单的动笔试一下,n < 4时,最大也凑不到24;n = 4时,1*2*3*4 = 24;n = 5时,3 * 5 + 2 * 4 + 1 = 24;n >= 6时,由于存在减法和乘法,于是我们只要先凑出24,然后将24重复乘以[n - (n - 1)]即可。
#include <iostream> #include <cstdio> #include <cstring> #include <string> using namespace std; int main() { int n; scanf("%d", &n); if(n < 4) {printf("NO\n"); goto loop;} else if(n % 2 == 0) { cout<<"YES"<<endl; cout<<"3 * 4 = 12"<<endl; cout<<"1 * 2 = 2"<<endl; cout<<"2 * 12 = 24"<<endl; for(int i = 5; i <= n; i += 2) { cout<<i+1<<" - "<<i<<" = 1"<<endl; cout<<"1 * 24 = 24"<<endl; } } else { cout<<"YES"<<endl; cout<<"3 * 5 = 15"<<endl; cout<<"2 * 4 = 8"<<endl; cout<<"15 + 8 = 23"<<endl; cout<<"23 + 1 = 24"<<endl; if(n == 5) goto loop; for(int i = 6; i <= n; i += 2) { cout<<i+1<<" - "<<i<<" = 1"<<endl; cout<<"1 * 24 = 24"<<endl; } } loop:; return 0; }
相关文章推荐
- 【Codeforces Round #268 (Div. 1)】A. 24 Game【归纳法】
- Codeforces Round #268 (Div. 1)A. 24 Game
- Codeforces Round #268 (Div. 2)+24 Game
- Codeforces Round #268 (Div. 1)A. 24 Game
- Codeforces Round#268(Div 2)C 24 Game
- Codeforces Round #268 (Div. 2) C 24 Game
- Codeforces Round #268 (Div. 1) A. 24 Game 构造
- Codeforces Round #268 (Div. 2) C 24 Game [构造]
- Codeforces Round #426 (Div. 2) C:The Meaningless Game(思维)
- Codeforces Round #267 (Div. 2) B. Fedor and New Game(位运算)
- LINTCODE——Compute 24 Game
- 679. 24 Game
- CF —— Codeforces Round #428 (Div. 2) B. Game of the Rows
- Codeforces Round #376 (Div. 2) E. Funny Game
- Codeforces Round #430 (Div. 2) E. Nikita and game
- Codeforces 469C 24 Game【思维+模拟】
- leetcode 24 Game
- 24 Game
- Codeforces Round #419 (Div. 2) C.Karen and Game 思维
- Educational Codeforces Round 24 E. Card Game Again(双指针)