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.ios

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.app

After n - 1 steps there is only one number left. Can you make this number equal to 24?ui

Input

The first line contains a single integer n (1 ≤ n ≤ 105).this

Output

If it's possible, print "YES" in the first line. Otherwise, print "NO" (without the quotes).spa

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.code

If there are multiple valid answers, you may print any of them.orm

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 。blog

看了別人的題解,這題只要最終考慮n=5或者n=4的狀況(看這代碼)  其他數 能夠經過 n-(n-1)=1來消掉, 而產生的1  能夠經過乘來消掉,每次n-=2,最終n==4或者n==5(由於不知道n是偶數仍是奇數)。ip

 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\n");
16     int cnt=0;
17     while(n>=6)
18     {
19         printf("%d - %d = %d\n",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\n");
27         printf("2 * 1 = 2\n");
28         printf("12 * 2 = 24\n");
29     }
30     else //n==5的狀況
31     {
32         printf("5 * 4 = 20\n");
33         printf("3 + 2 = 5\n");
34         printf("20 + 5 = 25\n");
35         printf("25 - 1 = 24\n");
36     }
37     while(cnt--)
38         printf("24 * 1 = 24\n"); //最後這個 把上面產生的1 所有用到
39 }
相關文章
相關標籤/搜索