zoukankan      html  css  js  c++  java
  • CodeForces

    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 ≤ 105).

    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.

    Examples

    Input
    1
    Output
    NO
    Input
    8
    Output
    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当中每次选两个数进行运算,进行n-1次运算后要为24,并且用过的数不能再用,新算出的数可以接着用,打印任意一种情况。

    思路:n小于4的一定不可以,n等于4和5需要特判,其他情况都可以转化为3-2=1,1-1=0开头,然后用0*i=0(i!=4&&i!=6),最后加个4*6=24就行了。
    代码:
     1 #include<stdio.h>
     2 #include<iostream>
     3 #include<string.h>
     4 using namespace std;
     5 int main()
     6 {
     7     int n;
     8     cin>>n;
     9     if(n<4)
    10     cout<<"NO"<<endl;
    11     else if(n==4)
    12     {
    13         cout<<"YES"<<endl;
    14         printf("1 * 2 = 2
    ");
    15         printf("2 * 3 = 6
    ");
    16         printf("4 * 6 = 24
    ");
    17     }
    18     else if(n==5)
    19     {
    20         cout<<"YES"<<endl;
    21         printf("3 * 5 = 15
    ");
    22         printf("2 * 4 = 8
    ");
    23         printf("15 + 8 = 23
    ");
    24         printf("23 + 1 = 24
    ");
    25     }
    26     else
    27     {
    28         cout<<"YES"<<endl;
    29         printf("3 - 2 = 1
    ");
    30         printf("1 - 1 = 0
    ");
    31         printf("0 * 5 = 0
    ");
    32         for(int i=7;i<=n;i++)
    33         if(i!=4&&i!=6)
    34         printf("0 * %d = 0
    ",i);
    35         printf("4 * 6 = 24
    ");
    36         printf("24 + 0 = 24
    ");
    37     }
    38     return 0;
    39 }


  • 相关阅读:
    try catch finally return
    github结合TortoiseGit使用sshkey,无需输入账号和密码
    github上fork别人的代码之后,如何保持和原作者同步的更新
    第9章 浅度和深度复制
    9.7结构类型
    excel在msdn上的说明文档
    9.6接口和抽象类
    [LeetCode]N-Queens II
    鸟哥Linux私房菜知识汇总8至9章
    Memcahce(MC)系列(三)Memcached它PHP转让
  • 原文地址:https://www.cnblogs.com/spongeb0b/p/9350131.html
Copyright © 2011-2022 走看看