码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces 468A 24 Game(构造)

时间:2014-09-24 23:48:18      阅读:335      评论:0      收藏:0      [点我收藏+]

标签:style   http   color   io   os   ar   for   sp   on   

题目链接:Codeforces 468A 24 Game

题目大意:给出n,表示有1~n这n个数,判断能否进n-1次操作获得24.

解题思路:4,5的情况可以手动处理出来,然后对于大于4,5的情况可以通过两两相减,形成若干个1.

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int main () {
    int n;
    scanf("%d", &n);
    if (n < 4)
        printf("NO\n");
    else {
        printf("YES\n");
        if (n&1) {
            printf("4 * 5 = 20\n");
            printf("20 + 3 = 23\n");
            printf("23 + 2 = 25\n");
            printf("25 - 1 = 24\n");

            for (int i = 6; i <= n; i += 2) {
                printf("%d - %d = 1\n", i + 1, i);
                printf("24 * 1 = 24\n");
            }

        } else {
            int c = 1;
            for (int i = 2; i <= 4; i++) {
                printf("%d * %d = %d\n", c, i, c * i);
                c = c * i;
            }

            for (int i = 5; i <= n; i += 2) {
                printf("%d - %d = 1\n", i + 1, i);
                printf("24 * 1 = 24\n");
            }
        }
    }
    return 0;
}

Codeforces 468A 24 Game(构造)

标签:style   http   color   io   os   ar   for   sp   on   

原文地址:http://blog.csdn.net/keshuai19940722/article/details/39528731

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!