题意:
让你从1~n中,每次从中选择两个数,进行+ 或 - 或 * 操作。然后从中删掉这两个数,把结果放回原集合中。通过n-1次的操作后,使得最后运算结果为24。
思路:
构造
1. n < 4 结果为NO
2. n 为 偶数, 把1~4构造成24,其他相邻两数相减变为1,最后输出多个24 * 1 = 24即可。
3. n 为奇数,把1~5构造成24, 其他相邻两数相减变为1,最后输出多个24 * 1 = 24即可。
AC代码:
/* **********************************************
Created Time: 2014-9-21 11:56:19
File Name : hehe.cpp
*********************************************** */
#include <iostream>
#include <fstream>
#include <cstring>
#include <climits>
#include <deque>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <functional>
#include <algorithm>
using namespace std;
typedef long long LL;
int n;
int main()
{
ios::sync_with_stdio(false);
cin>>n;
int cot = 0;
if(n < 4) {cout<<"NO"<<endl;}
else
{
cout<<"YES"<<endl;
if(n % 2 == 0)
{
cot += 3;
cout<<"1 * 2 = 2"<<endl;
cout<<"2 * 3 = 6"<<endl;
cout<<"6 * 4 = 24"<<endl;
for(int i = n; i >= 6; i-=2)
{
cot++;
cout<<i<<" - "<<i-1<<" = 1"<<endl;
}
for(int i = cot+1;i <= n-1; i++)
cout<<"24 * 1 = 24"<<endl;
}
else
{
cout<<"1 + 5 = 6"<<endl;
cout<<"4 * 6 = 24"<<endl;
cout<<"3 - 2 = 1"<<endl;
cout<<"1 * 24 = 24"<<endl;
cot += 4;
for(int i = n; i >= 7; i-=2)
{
cot++;
cout<<i<<" - "<<i-1<<" = 1"<<endl;
}
for(int i = cot+1; i <= n-1; i++)
cout<<"24 * 1 = 24"<<endl;
}
}
return 0;
}