codeforces a 24 game
来源:程序员人生 发布时间:2014-10-03 08:00:01 阅读次数:3545次
http://codeforces.com/contest/468/problem/A
其实一直不明白为啥程序员非常喜欢考智力题,可能算法=智力+数学?不过Codeforces的确挺好 代码短,挺像面试题什么的......
题目: n-1次操作凑出24 数不可以重复使用,操作可以是a-b a+b a*b
这个因为是DIV1的题,后来才发现可做
感觉有两种思路吧,凑0或者凑1
然后打印一堆24+0=24或者24*1=24
我凑一了
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <map>
#include <set>
#include <queue>
using namespace std;
#define ls(rt) rt*2
#define rs(rt) rt*2+1
#define ll long long
#define ull unsigned long long
#define rep(i,s,e) for(int i=s;i<e;i++)
#define repe(i,s,e) for(int i=s;i<=e;i++)
#define CL(a,b) memset(a,b,sizeof(a))
#define IN(s) freopen(s,"r",stdin)
#define OUT(s) freopen(s,"w",stdout)
const ll ll_INF = ((ull)(-1))>>1;
const double EPS = 1e-8;
const double pi = acos(-1.0);
const int INF = 100000000;
int main()
{
int n;
while(~scanf("%d",&n))
{
if(n<4){puts("NO");continue;}
printf("YES
");
if(n&1)
{
printf("5 * 3 = 15
");
printf("4 * 2 = 8
");
printf("15 + 8 = 23
");
printf("23 + 1 = 24
");
for(int i=7;i<=n;i+=2)
{
printf("%d - %d = 1
",i,i-1);
printf("24 * 1 = 24
");
}
}
else
{
printf("4 * 3 = 12
");
printf("12 * 2 = 24
");
printf("24 * 1 = 24
");
for(int i=6;i<=n;i+=2)
{
printf("%d - %d = 1
",i,i-1);
printf("24 * 1 = 24
");
}
}
}
return 0;
}
另一种凑0的思路 摘自http://blog.csdn.net/u011394362/article/details/39476329
#include <iostream>
using namespace std;
const int N = 1e3 + 5;
int main()
{
int n;
cin >> n;
if(n <= 3){
cout << "NO" << endl;
}
else if(n == 4){
cout << "YES" << endl;
cout << "2 * 3 = 6" << endl;
cout << "4 * 6 = 24" << endl;
cout << "1 * 24 = 24" << endl;
}
else if(n == 5){
cout << "YES" << endl;
cout << "5 - 2 = 3" << endl;
cout << "3 + 3 = 6" << endl;
cout << "4 * 6 = 24" << endl;
cout << "24 * 1 = 24" << endl;
}
else {
cout << "YES" << endl;
cout << "6 - 5 = 1" << endl;
cout << "1 - 1 = 0" << endl;
for(int i = 7; i <= n; i ++){
cout << i << " * 0 = 0" << endl;
}
cout << "2 * 3 = 6" << endl;
cout << "6 * 4 = 24" << endl;
cout << "24 + 0 = 24" << endl;
}
return 0;
}
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠