• 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


    题意:24点游戏,给出了你一个数字n,你需要在1-n的数字序列中选择两个数进行加,减,或乘法运算,然后运算出的结果也加入序列中,再选择两个数,进行同样的操作,问你能不能使得最后只剩下一个数,且那个数是24。若可以,输出YES以及每一步的运算公式。否则输出NO。

    思路:
    一开始被这题给吓到了,第一眼看起来很难,实际上却很简单。。。只要确保了一个24和一个0就行了。

    比如8,可以是4*6 = 24,1+2=3,3-3=0,这样就确保了 24 和 0 了,然后剩下的数都可以和 0 做乘法运算等到0(5*0=0,7*0=0,8*0=0),最后24+0=24就行了。

    所以,若n大于等于6,那一定可以通过上面的方法得出24,因为最小需要4*6=24。然后特判一下1-5就行了。

    1-3肯定是凑不出24的,然而4和5却可以,具体怎么凑的看代码:
     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #include<string>
     5 #include<cmath>
     6 #include<algorithm>
     7 #include<stack>
     8 #include<queue>
     9 #define eps 1e-7
    10 #define ll long long
    11 #define inf 0x3f3f3f3f
    12 #define pi 3.141592653589793238462643383279
    13 using namespace std;
    14 int main()
    15 {
    16     int n;
    17     while(cin>>n)
    18     {
    19         if(n >= 6)
    20         {
    21             cout<<"YES"<<endl;
    22             cout<<"1 + 2 = 3"<<endl;
    23             cout<<"3 - 3 = 0"<<endl;
    24             for(int i = 1; i<=n; ++i)
    25             {
    26                 if(i==1 || i==2 || i==3 || i==4 || i==6)
    27                     continue;
    28                 else
    29                     cout<<i<<" * 0 = 0"<<endl;
    30             }
    31             cout<<"0 + 4 = 4"<<endl;
    32             cout<<"4 * 6 = 24"<<endl;
    33         }
    34         else if(n == 4)
    35         {
    36             cout<<"YES"<<endl;
    37             cout<<"1 * 2 = 2"<<endl;
    38             cout<<"2 * 3 = 6"<<endl;
    39             cout<<"6 * 4 = 24"<<endl;
    40         }
    41         else if(n == 5)
    42         {
    43             cout<<"YES"<<endl;
    44             cout<<"3 * 5 = 15"<<endl;
    45             cout<<"2 * 4 = 8"<<endl;
    46             cout<<"1 + 8 = 9"<<endl;
    47             cout<<"9 + 15 = 24"<<endl;
    48         }
    49         else cout<<"NO"<<endl;
    50     }
    51     return 0;
    52 }
    
    
  • 相关阅读:
    路由器配置深入浅出—静态路由和缺省路由配置
    盘点飞思卡尔i.MX多媒体处理器前世今生 (转)
    ubuntu18.04下取消中键复制粘贴功能
    uboot常用命令及其使用
    MCU软件最佳实践——使用printf打印数据
    uboot无法通过nfs加载ubuntu18.04中的文件(转)
    GNU C字节对齐__attribute__((aligned(n))) #pragma pack(n)
    ENDIAN的由来及BIGEDIAN 和LITTLEENDIAN(转)
    自动生成c# Model属性
    使用JAVA生成随机的AES密钥
  • 原文地址:https://www.cnblogs.com/tuyang1129/p/9358688.html
Copyright © 2020-2023  润新知