zoukankan      html  css  js  c++  java
  • 24 Game

    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
    Copy
    1
    output
    NO
    input
    Copy
    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的数,通过+,-,*  的方式最终产生数字 24 。

    看了别人的题解,这题只要最终考虑n=5或者n=4的情况(看这代码)  其余数 可以通过 n-(n-1)=1来消掉, 而产生的1  可以通过乘来消掉,每次n-=2,最终n==4或者n==5(因为不知道n是偶数还是奇数)。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<cstring>
     5 using namespace std;
     6 
     7 int main()
     8 {
     9     int n;
    10     scanf("%d",&n);
    11     if(n<=3){
    12         printf("NO"); return 0;
    13     }
    14 
    15     printf("YES
    ");
    16     int cnt=0;
    17     while(n>=6)
    18     {
    19         printf("%d - %d = %d
    ",n,n-1,1);
    20         cnt++;  //统计 1的个数
    21         n-=2;
    22         
    23     }    //这里结束之后n==4或者n==5
    24     if(n==4)
    25     {
    26         printf("4 * 3 = 12
    ");
    27         printf("2 * 1 = 2
    ");
    28         printf("12 * 2 = 24
    ");
    29     }
    30     else //n==5的情况
    31     {
    32         printf("5 * 4 = 20
    ");
    33         printf("3 + 2 = 5
    ");
    34         printf("20 + 5 = 25
    ");
    35         printf("25 - 1 = 24
    ");
    36     }
    37     while(cnt--)
    38         printf("24 * 1 = 24
    "); //最后这个 把上面产生的1 全部用到
    39 }
  • 相关阅读:
    DMA
    [计网笔记] 应用层
    为博客添加网易云音乐播放器外链
    acm对拍程序 以及sublime text3的文件自动更新插件auto refresh
    操作系统---内存管理(上) 概念 覆盖交换技术 连续分配管理方式
    操作系统---死锁的检测和解除
    C++ bitset的使用
    Codeforces Round #647 (Div. 2)
    sublime Text3 实现2:1:1三分屏效果
    操作系统---信号量以及四个进程同步问题
  • 原文地址:https://www.cnblogs.com/thunder-110/p/8504758.html
Copyright © 2011-2022 走看看