题目链接: 传送门

24 Game

time limit per test:1 second     memory limit per test:256 megabytes

Description

Little X used to play a card game called "24 Game", but recently he has found it too easy. So he invented a new game.
Initially you have a sequence of n integers: 1, 2, ..., n. In a single step, you can pick two of them, let's denote them a and b, erase them from the sequence, and append to the sequence either a + b, or a - b, or a × b.
After n - 1 steps there is only one number left. Can you make this number equal to 24?

Input

The first line contains a single integer n (1 ≤ n ≤ 10^5).

Output

If it's possible, print "YES" in the first line. Otherwise, print "NO" (without the quotes).
If there is a way to obtain 24 as the result number, in the following n - 1 lines print the required operations an operation per line. Each operation should be in form: "a op b = c". Where a and b are the numbers you've picked at this operation; op is either "+", or "-", or "*"; c is the result of corresponding operation. Note, that the absolute value of c mustn't be greater than 1018. The result of the last operation must be equal to 24. Separate operator sign and equality sign from numbers with spaces.
If there are multiple valid answers, you may print any of them.

Sample Input

1

8

Sample Output

NO

YES
8 * 7 = 56
6 * 5 = 30
3 - 4 = -1
1 - 2 = -1
30 - -1 = 31
56 - 31 = 25
25 + -1 = 24

解题思路:

题目大意:给一个数n,问能不能由1~n通过加减乘除组成24
解题思路:If n ≤ 3, it's easy to find that it's impossible to make 24, because the maximal number they can form is 9.
If n > 5, we can simply add n - (n - 1) = 1, 24  1 = 24 at the end of the solution to the number n - 2.
So we can find the solution of 4, 5 by hand. 1 
 2  3  4 = 24, (5 - 3)  4  (2 + 1) = 24
另外,在n > 5时,可以通过1+5 = 6,6-6=0,0x = 0,再由23*4=24.

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

int main()
{
    int N;
    while (~scanf("%d",&N))
    {
        if (N < 4)
        {
            printf("NO\n");
            continue;
        }
        if (N == 4)
        {
            printf("YES\n");
            printf("1 * 2 = 2\n");
            printf("2 * 3 = 6\n");
            printf("4 * 6 = 24\n");
        }
        else if (N == 5)
        {
            printf("YES\n");
            printf("5 - 3 = 2\n");
            printf("2 + 1 = 3\n");
            printf("2 * 3 = 6\n");
            printf("6 * 4 = 24\n");
        }
        else if (N > 5)
        {
            printf("YES\n");
            printf("5 + 1 = 6\n");
            printf("6 - 6 = 0\n");
            for (int i = 7;i <= N;i++)
            {
                printf("0 * %d = 0\n",i);
            }
            printf("2 + 0 = 2\n");
            printf("2 * 3 = 6\n");
            printf("6 * 4 = 24\n");
        }
    }
    return 0;
}

CF 468A 24 Game的更多相关文章

  1. codeforces 468A. 24 Game 解题报告

    题目链接:http://codeforces.com/problemset/problem/468/A 题目意思:给出一个数n,利用 1 - n 这 n 个数,每个数只能用一次,能否通过3种运算: + ...

  2. [ An Ac a Day ^_^ ] CodeForces 468A 24 Game 构造

    题意是让你用1到n的数构造24 看完题解感觉被样例骗了…… 很明显 n<4肯定不行 然后构造出来4 5的组成24的式子 把大于4(偶数)或者5(奇数)的数构造成i-(i-1)=1 之后就是无尽的 ...

  3. 译文 - Recommender Systems: Issues, Challenges, and Research Opportunities

    REF: 原文 Recommender Systems: Issues, Challenges, and Research Opportunities Shah Khusro, Zafar Ali a ...

  4. Hadoop+HBase 集群搭建

    Hadoop+HBase 集群搭建 1. 环境准备 说明:本次集群搭建使用系统版本Centos 7.5 ,软件版本 V3.1.1. 1.1 配置说明 本次集群搭建共三台机器,具体说明下: 主机名 IP ...

  5. https传输过程嗅探

    C1->浏览器告知服务器自身的信息 length = 165 a5 16 03 01 00 A0 01 00 00 9C 03 03 5E 1C 37 CD 40 [ ^ 7 @] B6 4A ...

  6. hBase官方文档以及HBase基础操作封装类

    HBase 官方文档 0.97 http://abloz.com/hbase/book.html HBase基本操作封装类(以课堂爬虫为例) package cn.crxy.spider.utils; ...

  7. hbase 增删改查 api 简单操作

    package com.utils; import java.io.IOException; import java.util.ArrayList; import java.util.List; im ...

  8. python3.7+flask+alipay 支付宝付款功能

    文档参考github:https://github.com/fzlee/alipay/blob/master/docs/init.md 沙箱环境配置:https://opendocs.alipay.c ...

  9. [ 9.24 ]CF每日一题系列—— 468A构造递推

    Description: 1 - n个数问你能否经过加减乘除这些运算n -1次的操作得到24 Solutrion: 一开始想暴力递推,发现n的范围太大直接否决,也否决了我的跑dfs,后来就像肯定有个递 ...

随机推荐

  1. BatsingJSLib 2.3、Ajax上传多个文件

    //2.3Ajax上传单个或多个文件 //<input type="file" multiple="multiple"/> //参数:文件的表单JD ...

  2. [MCSM] Slice Sampler

    1. 引言 之前介绍的MCMC算法都具有一般性和通用性(这里指Metropolis-Hasting 算法),但也存在一些特殊的依赖于仿真分布特征的MCMC方法.在介绍这一类算法(指Gibbs samp ...

  3. .net异步编程

    现在电脑大部分都是多核心,在处理多线程方便有很大优势,异步调用方法的时候可以立即返回执行其他程序,进行异步编程会让程序运行效率更高. 我也是刚刚关注异步编程方面知识,也有很多不是很理解,所以想向大神请 ...

  4. swagger editor使用

    swagger editor使用 swagger是一套开源的API设计工具,包括Swagger UI,Swagger Editor等. Swagger Editor 其中Swagger Editor是 ...

  5. js自定义事件

    自定义事件的本质,创建一个对象,然后把事件的名字作为对象的一个属性,然后value是一个[],把此事件的所以回调都push进去. 写一个很基本的,没有把对象暴露出去的js的自定义事件. var eve ...

  6. pay-as-you-go

    What is pay as you go? A pay as you go deal means you aren’t tied into a contract and can top up you ...

  7. matlab画图形函数 semilogx

    matlab画图形函数 semilogx loglog 主要是学习semilogx函数,其中常用的是semilogy函数,即后标为x的是在x轴取对数,为y的是y轴坐标取对数.loglog是x y轴都取 ...

  8. TAR命令详解

    上图,VPN截图,画蛇添足! 在Linux中,压缩与解压用得最多的tar.tar命令确实很厉害. tar -c: 建立压缩档案-x:解压-t:查看内容-r:向压缩归档文件末尾追加文件-u:更新原压缩包 ...

  9. go-- 用go-mssql驱动连接sqlserver数据库

    import _ "github.com/denisenkom/go-mssqldb" import ( "crypto/cipher" "crypt ...

  10. Keepalived+Redis高可用部署

    1   Redis简介及安装 Redis是一个开源,先进的key-value存储,并用于构建高性能,可扩展的Web应用程序的完美解决方案. Redis从它的许多竞争继承来的三个主要特点: Redis数 ...