Get the App
SLTechnology News&Howtos  ›  Development  › 

How does C++ realize the program of calculating 24:00

Shulou Source: shulou.com Published: 2022-05-31 22:11:06 10月04日 Update

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.

Tags: Programs content clocking articles knowledge articles code values most of them that is integers more steps knowledge points industry information information channels channels help combinations Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker vpn Apple NVidia Shulou Technology