Link: http://codeforces.com/problemset/problem/468/A
A. 24 gametime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output
Little X used to play a card game called "24 game", but recently he has found it too easy. So he has Ted a new game.
Initially you have a sequenceNIntegers: 1 ,? 2 ,?...,?N. In a single step, you can pick two of them, let's denote themAAndB, Erase them from the sequence, and append to the sequence eitherA? +?B, OrA? -?B, OrA? ×?B.
AfterN? -? 1 steps there is only one number left. Can you make this number equal to 24?
Input
The first line contains a single integerN(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 followingN? -? 1 lines print the required operations an operation per line. Each operation shoshould be in Form :"A OP B=C". WhereAAndBAre the numbers you 've picked at this operation;OPIs either "+", or "-", or "*";CIs the result of corresponding operation. Note, that the absolute valueCMustn'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 test (s) Input
1
Output
NO
Input
8
Output
YES8 * 7 = 566 * 5 = 303 - 4 = -11 - 2 = -130 - -1 = 3156 - 31 = 2525 + -1 = 24
Ideas:
There are many kinds of answers, but the ultimate goal is to construct a 24-point structure. Obviously, less than 4 cannot be constructed successfully. For numbers greater than or equal to 4, we first create the constructor when n is equal to 4 and 5 respectively, then convert all the numbers greater than 5 (printf ("% d-% d = 1 \ n", I, I-1);) to 1, then printf ("24*1 = 24 \ n"); in this way, only 24 points are retained!
The Code is as follows:
# Include <cstdio> using namespace STD; int main () {int N; while (~ Scanf ("% d", & N) {If (n <4) {printf ("no"); continue;} printf ("Yes \ n "); if (N & 1) // odd {printf ("3 + 4 = 7 \ n"); printf ("7 + 5 = 12 \ n "); printf ("12*2 = 24 \ n"); printf ("24*1 = 24 \ n ");} else {printf ("1*2 = 2 \ n"); printf ("2*3 = 6 \ n "); printf ("6*4 = 24 \ n");} For (INT I = N; I> 5; I-= 2) // convert all to 1 {printf ("% d-% d = 1 \ n", I, I-1 ); printf ("24*1 = 24 \ n") ;}} return 0 ;}
Codeforces 468a. 24 game (mathematical structure)