当前位置:   article > 正文

计算24(蓝桥杯)_蓝桥杯24点计算器c++

蓝桥杯24点计算器c++

“计算 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。

对于此题:大家应该暴力写代码都会了,但是本人在这里是要介绍简写代码的方法。

1.复制法:

首先,先写下以上代码:

  1. if (a + b + c + d == 24 ) sum ++;
  2. if (a + b + c - d == 24 ) sum ++;
  3. if (a + b + c * d == 24 ) sum ++;
  4. if (a + b - c + d == 24 ) sum ++;
  5. if (a + b - c - d == 24 ) sum ++;
  6. if (a + b - c * d == 24 ) sum ++;
  7. if (a + b * c + d == 24 ) sum ++;
  8. if (a + b * c - d == 24 ) sum ++;
  9. if (a + b * c * d == 24 ) sum ++;

再把每组中的第一个“+”号,分别改成减号和乘号就欧啦。

2.代码法:

作为一个懒人,连复制都懒得复制了。

于是呼:用代码帮我们写代码。

首先:先打开另一个c++文件,插入以下代码:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define MAXN 1048586
  5. #define M 1005
  6. #define N 500005
  7. #define lowbit(x) ((x)&(-(x)))
  8. #define debug(...) cout << '[' << 'D' << 'E' << ']' << ' ' << __LINE__ << ':' << __VA_ARGS__ << endl;
  9. #define vl(x) #x << '(' << x << ')'
  10. #define mod 998244353
  11. #define who(s,limit,tpe) s + tpe , s + limit + tpe
  12. #define all(s) s.begin(),s.end()
  13. #define s_(s) s.size()
  14. int a[N];
  15. string s[M] = {"","+++","++-","++*","+-+","+--","+-*","+*+","+*-","+**","-++","-+-","-+*","--+","---","--*","-*+","-*-","-**","*++","*+-","*+*","*-+","*--","*-*","**+","**-","***"};
  16. vector<int> G[N];
  17. int main(){
  18. int n;
  19. ios::sync_with_stdio(false);
  20. cin.tie(0);
  21. cout.tie(0);
  22. for(int i = 1; i <= 27; i ++){
  23. cout << " " << "if (a " << s[i][0] << " b " << s[i][1] << " c " << s[i][2] << " d == 24 ) sum ++;" << endl ;
  24. }
  25. }

你会惊奇的发现:输出的代码竟是我们需要的代码!

我们要多利用代码帮我们写代码,做到多偷懒。^_^

  1. // 输出
  2. if (a + b + c + d == 24 ) sum ++;
  3. if (a + b + c - d == 24 ) sum ++;
  4. if (a + b + c * d == 24 ) sum ++;
  5. if (a + b - c + d == 24 ) sum ++;
  6. if (a + b - c - d == 24 ) sum ++;
  7. if (a + b - c * d == 24 ) sum ++;
  8. if (a + b * c + d == 24 ) sum ++;
  9. if (a + b * c - d == 24 ) sum ++;
  10. if (a + b * c * d == 24 ) sum ++;
  11. if (a - b + c + d == 24 ) sum ++;
  12. if (a - b + c - d == 24 ) sum ++;
  13. if (a - b + c * d == 24 ) sum ++;
  14. if (a - b - c + d == 24 ) sum ++;
  15. if (a - b - c - d == 24 ) sum ++;
  16. if (a - b - c * d == 24 ) sum ++;
  17. if (a - b * c + d == 24 ) sum ++;
  18. if (a - b * c - d == 24 ) sum ++;
  19. if (a - b * c * d == 24 ) sum ++;
  20. if (a * b + c + d == 24 ) sum ++;
  21. if (a * b + c - d == 24 ) sum ++;
  22. if (a * b + c * d == 24 ) sum ++;
  23. if (a * b - c + d == 24 ) sum ++;
  24. if (a * b - c - d == 24 ) sum ++;
  25. if (a * b - c * d == 24 ) sum ++;
  26. if (a * b * c + d == 24 ) sum ++;
  27. if (a * b * c - d == 24 ) sum ++;
  28. if (a * b * c * d == 24 ) sum ++;

所以,代码为:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define MAXN 1048586
  5. #define M 1005
  6. #define N 500005
  7. #define lowbit(x) ((x)&(-(x)))
  8. #define debug(...) cout << '[' << 'D' << 'E' << ']' << ' ' << __LINE__ << ':' << __VA_ARGS__ << endl;
  9. #define vl(x) #x << '(' << x << ')'
  10. #define mod 998244353
  11. #define who(s,limit,tpe) s + tpe , s + limit + tpe
  12. #define all(s) s.begin(),s.end()
  13. #define s_(s) s.size()
  14. int a[N];
  15. string s;
  16. vector<int> G[N];
  17. int main(){
  18. int n;
  19. ios::sync_with_stdio(false);
  20. cin.tie(0);
  21. cout.tie(0);
  22. int a,b,c,d;
  23. cin >> a >> b >> c >> d;
  24. int sum = 0 ;
  25. if (a + b + c + d == 24 ) sum ++;
  26. if (a + b + c - d == 24 ) sum ++;
  27. if (a + b + c * d == 24 ) sum ++;
  28. if (a + b - c + d == 24 ) sum ++;
  29. if (a + b - c - d == 24 ) sum ++;
  30. if (a + b - c * d == 24 ) sum ++;
  31. if (a + b * c + d == 24 ) sum ++;
  32. if (a + b * c - d == 24 ) sum ++;
  33. if (a + b * c * d == 24 ) sum ++;
  34. if (a - b + c + d == 24 ) sum ++;
  35. if (a - b + c - d == 24 ) sum ++;
  36. if (a - b + c * d == 24 ) sum ++;
  37. if (a - b - c + d == 24 ) sum ++;
  38. if (a - b - c - d == 24 ) sum ++;
  39. if (a - b - c * d == 24 ) sum ++;
  40. if (a - b * c + d == 24 ) sum ++;
  41. if (a - b * c - d == 24 ) sum ++;
  42. if (a - b * c * d == 24 ) sum ++;
  43. if (a * b + c + d == 24 ) sum ++;
  44. if (a * b + c - d == 24 ) sum ++;
  45. if (a * b + c * d == 24 ) sum ++;
  46. if (a * b - c + d == 24 ) sum ++;
  47. if (a * b - c - d == 24 ) sum ++;
  48. if (a * b - c * d == 24 ) sum ++;
  49. if (a * b * c + d == 24 ) sum ++;
  50. if (a * b * c - d == 24 ) sum ++;
  51. if (a * b * c * d == 24 ) sum ++;
  52. cout << sum << endl ;
  53. }

The End.

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/64320
推荐阅读
相关标签
  

闽ICP备14008679号