首页 > 代码库 > Codeforces 468A 24 Game(构造)

Codeforces 468A 24 Game(构造)

题目链接: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(构造)