赞
踩
“计算 24”是一个流传已久的数字游戏,小蓝最近对此痴迷不已。
游戏规则是:对 4 个 1-10 之间的自然数,进行加、减、乘三种运算,要求运算结果等于 24。乘法的优先级高于加、减,并且算式中不可以用括号,不可以改变 4 个数字出现的顺序。
下面我们给出两个游戏的具体例子:
若给出的 4 个操作数是:10、2、4、8,则有两种可能的解答方案:
10+2+4+8=24,10*2-4+8=24,输出内容:2
若给出的 4 个操作数是:7、2、3、6,则没有解答案,输出内容:0。
首先,先写下以上代码:
- if (a + b + c + d == 24 ) sum ++;
- if (a + b + c - d == 24 ) sum ++;
- if (a + b + c * d == 24 ) sum ++;
- if (a + b - c + d == 24 ) sum ++;
- if (a + b - c - d == 24 ) sum ++;
- if (a + b - c * d == 24 ) sum ++;
- if (a + b * c + d == 24 ) sum ++;
- if (a + b * c - d == 24 ) sum ++;
- if (a + b * c * d == 24 ) sum ++;
再把每组中的第一个“+”号,分别改成减号和乘号就欧啦。
作为一个懒人,连复制都懒得复制了。
于是呼:用代码帮我们写代码。
首先:先打开另一个c++文件,插入以下代码:
- #include<bits/stdc++.h>
- using namespace std;
- #define ll long long
- #define MAXN 1048586
- #define M 1005
- #define N 500005
- #define lowbit(x) ((x)&(-(x)))
- #define debug(...) cout << '[' << 'D' << 'E' << ']' << ' ' << __LINE__ << ':' << __VA_ARGS__ << endl;
- #define vl(x) #x << '(' << x << ')'
- #define mod 998244353
- #define who(s,limit,tpe) s + tpe , s + limit + tpe
- #define all(s) s.begin(),s.end()
- #define s_(s) s.size()
- int a[N];
- string s[M] = {"","+++","++-","++*","+-+","+--","+-*","+*+","+*-","+**","-++","-+-","-+*","--+","---","--*","-*+","-*-","-**","*++","*+-","*+*","*-+","*--","*-*","**+","**-","***"};
- vector<int> G[N];
- int main(){
- int n;
- ios::sync_with_stdio(false);
- cin.tie(0);
- cout.tie(0);
- for(int i = 1; i <= 27; i ++){
- cout << " " << "if (a " << s[i][0] << " b " << s[i][1] << " c " << s[i][2] << " d == 24 ) sum ++;" << endl ;
- }
- }
你会惊奇的发现:输出的代码竟是我们需要的代码!
我们要多利用代码帮我们写代码,做到多偷懒。^_^
- // 输出
- if (a + b + c + d == 24 ) sum ++;
- if (a + b + c - d == 24 ) sum ++;
- if (a + b + c * d == 24 ) sum ++;
- if (a + b - c + d == 24 ) sum ++;
- if (a + b - c - d == 24 ) sum ++;
- if (a + b - c * d == 24 ) sum ++;
- if (a + b * c + d == 24 ) sum ++;
- if (a + b * c - d == 24 ) sum ++;
- if (a + b * c * d == 24 ) sum ++;
- if (a - b + c + d == 24 ) sum ++;
- if (a - b + c - d == 24 ) sum ++;
- if (a - b + c * d == 24 ) sum ++;
- if (a - b - c + d == 24 ) sum ++;
- if (a - b - c - d == 24 ) sum ++;
- if (a - b - c * d == 24 ) sum ++;
- if (a - b * c + d == 24 ) sum ++;
- if (a - b * c - d == 24 ) sum ++;
- if (a - b * c * d == 24 ) sum ++;
- if (a * b + c + d == 24 ) sum ++;
- if (a * b + c - d == 24 ) sum ++;
- if (a * b + c * d == 24 ) sum ++;
- if (a * b - c + d == 24 ) sum ++;
- if (a * b - c - d == 24 ) sum ++;
- if (a * b - c * d == 24 ) sum ++;
- if (a * b * c + d == 24 ) sum ++;
- if (a * b * c - d == 24 ) sum ++;
- if (a * b * c * d == 24 ) sum ++;
所以,代码为:
- #include<bits/stdc++.h>
- using namespace std;
- #define ll long long
- #define MAXN 1048586
- #define M 1005
- #define N 500005
- #define lowbit(x) ((x)&(-(x)))
- #define debug(...) cout << '[' << 'D' << 'E' << ']' << ' ' << __LINE__ << ':' << __VA_ARGS__ << endl;
- #define vl(x) #x << '(' << x << ')'
- #define mod 998244353
- #define who(s,limit,tpe) s + tpe , s + limit + tpe
- #define all(s) s.begin(),s.end()
- #define s_(s) s.size()
- int a[N];
- string s;
- vector<int> G[N];
- int main(){
- int n;
- ios::sync_with_stdio(false);
- cin.tie(0);
- cout.tie(0);
- int a,b,c,d;
- cin >> a >> b >> c >> d;
- int sum = 0 ;
- if (a + b + c + d == 24 ) sum ++;
- if (a + b + c - d == 24 ) sum ++;
- if (a + b + c * d == 24 ) sum ++;
- if (a + b - c + d == 24 ) sum ++;
- if (a + b - c - d == 24 ) sum ++;
- if (a + b - c * d == 24 ) sum ++;
- if (a + b * c + d == 24 ) sum ++;
- if (a + b * c - d == 24 ) sum ++;
- if (a + b * c * d == 24 ) sum ++;
- if (a - b + c + d == 24 ) sum ++;
- if (a - b + c - d == 24 ) sum ++;
- if (a - b + c * d == 24 ) sum ++;
- if (a - b - c + d == 24 ) sum ++;
- if (a - b - c - d == 24 ) sum ++;
- if (a - b - c * d == 24 ) sum ++;
- if (a - b * c + d == 24 ) sum ++;
- if (a - b * c - d == 24 ) sum ++;
- if (a - b * c * d == 24 ) sum ++;
- if (a * b + c + d == 24 ) sum ++;
- if (a * b + c - d == 24 ) sum ++;
- if (a * b + c * d == 24 ) sum ++;
- if (a * b - c + d == 24 ) sum ++;
- if (a * b - c - d == 24 ) sum ++;
- if (a * b - c * d == 24 ) sum ++;
- if (a * b * c + d == 24 ) sum ++;
- if (a * b * c - d == 24 ) sum ++;
- if (a * b * c * d == 24 ) sum ++;
- cout << sum << endl ;
- }
The End.
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。