A. 24 Game
Time Limit: 1 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/contest/468/problem/ADescription
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.
Sample Input
8
Sample Output
YES
8 * 7 = 56
6 * 5 = 30
3 - 4 = -1
1 - 2 = -1
30 - -1 = 31
56 - 31 = 25
25 + -1 = 24
HINT
题意
给你1到n,的n个数,你可以挑选两个数出来进行加减乘除,然后再把这俩数擦去,然后再把新得到的数扔进去,问你最后剩下的数是不是24
题解:
构造题,小于三个肯定不行了,因为乘起来才12……
大于等于4个就可行了,因为1*2*3*4 = 24,后面的数都相减为1 就好了
奇数也可以构造 (3-1)*2*5+4=24,然后后面的数都减1就好了
代码:
//qscqesze #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <bitset> #include <vector> #include <sstream> #include <queue> #include <typeinfo> #include <fstream> #include <map> #include <stack> typedef long long ll; using namespace std; //freopen("D.in","r",stdin); //freopen("D.out","w",stdout); #define sspeed ios_base::sync_with_stdio(0);cin.tie(0) #define maxn 100006 #define mod 1000000007 #define eps 1e-9 #define PI acos(-1) const double EP = 1E-10 ; int Num; //const int inf=0x7fffffff; const ll inf=999999999; inline ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } //************************************************************************************* int main() { int n=read(); if(n<4)cout<<"NO"<<endl; else { cout<<"YES"<<endl; for(;n-2>=4;n-=2) cout<<n<<" - "<<n-1<<" = 1 1 * 1 = 1 "; if(n==4) { cout<<"1 * 2 = 2"<<endl; cout<<"2 * 3 = 6"<<endl; cout<<"6 * 4 = 24"<<endl; } else { cout<<"3 - 1 = 2"<<endl; cout<<"2 * 2 = 4"<<endl; cout<<"4 * 5 = 20"<<endl; cout<<"20 + 4 = 24"<<endl; } } }