In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Most people do not understand the knowledge points of this article "how to achieve the 24:00 calculation program on C++", so the editor summarizes the following contents for you. The contents are detailed, the steps are clear, and they can be used for reference. I hope you can get something after reading this article. Let's take a look at this "C++ how to achieve the calculation of 24:00 program" article.
Simple program:
1. Run the program
two。 Enter 4 integers, for example: 3 3 7 8
3. Show all possible combinations
Code:
# include "assert.h" # include double operate (double num1, double num2, int op) {assert (op > = 0 & & op)
< 4); if(op == 0){ return num1 + num2; } else if(op == 1){ return num1 - num2; } else if(op == 2){ return num1 * num2; } else{ return num1/num2; }} int calculate(int num1, int num2, int num3, int num4){ char operators[] = "+-*/"; for(int i = 0; i < 4; i ++) { for(int j = 0; j < 4; j ++) { for (int k = 0; k < 4; k ++) { double ret = operate(num1, num2, i); ret = operate(ret, num3, j); ret = operate(ret, num4, k); if(abs(ret - 24) < 0.001){ printf("((%d %c %d) %c %d) %c %d = %f\n", num1, operators[i], num2, operators[j], num3, operators[k], num4, ret); } ret = operate(num1, num2, i); double ret2 = operate(num3, num4, k); ret = operate(ret, ret2, j); if(abs(ret - 24) < 0.001){ printf("(%d %c %d) %c (%d %c %d) = %f\n", num1, operators[i], num2, operators[j], num3, operators[k], num4, ret); } ret = operate(num2, num3, j); ret = operate(num1, ret, i); ret = operate(ret, num4, k); if(abs(ret - 24) < 0.001){ printf("(%d %c (%d %c %d)) %c %d = %f\n", num1, operators[i], num2, operators[j], num3, operators[k], num4, ret); } ret = operate(num2, num3, j); ret = operate(ret, num4, k); ret = operate(num1, ret, i); if(abs(ret - 24) < 0.001){ printf("%d %c ((%d %c %d) %c %d) = %f\n", num1, operators[i], num2, operators[j], num3, operators[k], num4, ret); } ret = operate(num3, num4, k); ret = operate(num2, ret, j); ret = operate(num1, ret, i); if(abs(ret - 24) < 0.001){ printf("%d %c (%d %c (%d %c %d)) = %f\n", num1, operators[i], num2, operators[j], num3, operators[k], num4, ret); } } } } return 0;} int main(int argc, char* argv[]){ int nums[4] = {0, 0, 0, 0}; std::cin >> nums [0] > > nums [1] > > nums [2] > > nums [3]; for (int I = 0; I < sizeof (nums) / sizeof (nums [0]); I + +) {int num1 = nums [I]; int ret = num1; for (int j = 0; j < sizeof (nums) / sizeof (nums [0]); j + +) {if (j = I) continue; int num2 = nums [j]; for (int k = 0) K < sizeof (nums) / sizeof (nums [0]); knight +) {if (k = = I | | k = = j) continue; int num3 = nums [k]; for (int l = 0; l < sizeof (nums) / sizeof (nums [0]); l + +) {if (l = I | | l = j | l = k) continue; int num4 = nums [l]; calculate (num1, num2, num3, num4);} return 0 } the above is about the content of this article on "how to achieve the program of calculating 24:00 on C++". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about it, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.