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
The first line contains a single integer n (1 ≤ n ≤ 105).this
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
1
NO
8
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 }