codeforces a 24 game
來源:程序員人生 發布時間:2014-10-03 08:00:01 閱讀次數:3825次
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;
}
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈