Codeforces Round#268(Div 2)C 24 Game

题目链接:【戳这里】

题目大意:初始你手中有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;
}


posted @ 2014-09-22 09:08  gaoxiang36999  阅读(198)  评论(0)    收藏  举报