如何创建所需数量的嵌套循环? 我正在尝试创建一个程序,可以在C++语言中按我想要的方式使用许多数字。 然后找出哪些算子可以使方程成立,并显示所有可能的正确运算。 示例:如果我把3 5 15 然后输出3x5=15 如果我把12344 然后输出1+2-3+4=4

如何创建所需数量的嵌套循环? 我正在尝试创建一个程序,可以在C++语言中按我想要的方式使用许多数字。 然后找出哪些算子可以使方程成立,并显示所有可能的正确运算。 示例:如果我把3 5 15 然后输出3x5=15 如果我把12344 然后输出1+2-3+4=4,c++,recursion,nested-loops,C++,Recursion,Nested Loops,以下代码是我编写的程序: 问题是,当我想减少输入数量或增加输入数量时,每次都需要添加/减少嵌套循环。我想知道更灵活的嵌套循环或递归方法的更有效方法是什么 #include <iostream> #include <cmath> using namespace std; char getOperator(int); double operate(int, double, double); int main() { double a, b, c, d, e,

以下代码是我编写的程序: 问题是,当我想减少输入数量或增加输入数量时,每次都需要添加/减少嵌套循环。我想知道更灵活的嵌套循环或递归方法的更有效方法是什么

#include <iostream>
#include <cmath>

using namespace std;


char getOperator(int);
double operate(int, double, double);

int main() {
    double a, b, c, d, e, result;
    short noOfAnswers = 0;
    cout << "Input first 5 numbers to make it equal to another 1 number.\n" <<
    "I'll find what are the operators needed to make 2 sides of the equation equal.\n";
    cin >> a >> b >> c >> d >> e >> result;
    int noOfOperators = 5;
    for (int i = 0; i <= noOfOperators; i++) {
        double firstTwo = operate(i, a, b);
        for (int j = 0; j <= noOfOperators; j++) {
            double firstThree = operate(j, firstTwo, c);
            for (int k = 0; k <= noOfOperators; k++) {
                double firstFour = operate(k, firstThree, d);
                for (int l = 0; l <= noOfOperators; l++) {
                    double firstFive = operate(l, firstFour, e);
                    if (firstFive == result) {
                        cout << ++noOfAnswers << ')' << a << getOperator(i) << b << getOperator(j) << c
                        << getOperator(k) << d << getOperator(l) << e << '=' << result << endl;
                    }
                }
            }
        }
    }
    if (noOfAnswers) cout << "I have found " << noOfAnswers << " solutions for this extremely hard problem for humanity \nin less than a second." << endl;
    else cout << "I cannot find any solutions to this problem.\n"
    <<"They're just a bunch of random numbers & That is UNSOLVABLE!" << endl;
    cout << "Do not doubt my judgment. I am always right!" << endl << "(Please note that all calculations are done from the left side first.)" << endl;
    return 0;
}

double operate(int iteration, double num1, double num2) {
    switch (iteration) {
        case 0: return num1+num2;
        case 1: return num1-num2;
        case 2: return num1*num2;
        case 3: return num1/num2;
        case 4: return pow(num1, num2);
        case 5: return fmod(num1, num2);
    }
    return 0;
}

char getOperator(int pos) {
    switch (pos) {
        case 0: return '+';
        case 1: return '-';
        case 2: return 'x';
        case 3: return '/';
        case 4: return '^';
        case 5: return '%';
    }
    return ' ';
}
#包括
#包括
使用名称空间std;
字符运算符(int);
双操作(int、double、double);
int main(){
双a,b,c,d,e,结果;
短noOfAnswers=0;
cout a>>b>>c>>d>>e>>结果;
int noOfOperators=5;
对于(int i=0;i以下内容可能会有所帮助:

// increment v where each value is a digit with maximal value maxSize
// so {0,1,2}, 3 lead to {0,2,0}
// return false on overflow.
bool increment(std::vector<int>& v, int maxSize)
{
    for (auto it = v.rbegin(); it != v.rend(); ++it) {
        ++*it;
        if (*it != maxSize) {
            return true;
        }
        *it = 0;
    }
    return false;
}

// display something like 1 + 2 * 3 = 9 // with the following meaning ((1 + 2) * 3) = 9
void display(const std::vector<double>& operands, const std::vector<int>& operators, double total)
{
    const char operators_string[] = "+-*/^%";

    std::cout << operands[0];
    for (std::size_t i = 0; i != operators.size(); ++i) {
        std::cout << " " << operators_string[operators[i]] << " " << operands[i + 1];
    }
    std::cout << " = " << total << std::endl;
}

// Compute something like {1 2 3 4}, {+ * /} as (((1 + 2) * 3) / 4)
double compute(const std::vector<double>& operands, const std::vector<int>& operators)
{
    std::function<double(double, double)> fs[] = {
        [](double a, double b) { return a + b; },
        [](double a, double b) { return a - b; },
        [](double a, double b) { return a * b; },
        [](double a, double b) { return a / b; },
        [](double a, double b) { return pow(a, b); },
        [](double a, double b) { return fmod(a, b); },
    };

    double res = operands[0];
    for (std::size_t i = 0; i != operators.size(); ++i) {
        res = fs[operators[i]](res, operands[i + 1]);
    }
    return res;
}

void display_combinaison(const std::vector<double>& operands, double total)
{
    std::vector<int> operators(operands.size() - 1);

    do {
        if (compute(operands, operators) == total) {
            display(operands, operators, total);
        }
    } while (increment(operators, 6));
}
//增量v,其中每个值都是一个最大值为maxSize的数字
//所以{0,1,2},3导致{0,2,0}
//溢出时返回false。
布尔增量(标准::向量&v,整数最大值)
{
对于(自动it=v.rbegin();it!=v.rend();++it){
++*它;
如果(*it!=maxSize){
返回true;
}
*它=0;
}
返回false;
}
//显示类似于1+2*3=9//的内容,其含义如下((1+2)*3)=9
无效显示(常量标准::向量和操作数,常量标准::向量和运算符,双倍合计)
{
常量字符运算符\u字符串[]=“+-*/^%”;

std::cout您可能希望使用while()循环,因为您不知道循环何时终止

int main() {
    double numbers[] = {3,5,15};//consider storing the number as an array
    //the last element is the result
    double result;
    int arr_len = sizeof(numbers)/sizeof(double);
    int i,j;

    while(1)
    {
          j = 0;
          while(j++ < 5)//over all operators
          {i = 0;
              result = numbers[0];//start with first element
              while(i < arrlen - 2)//over all numbers, exclude the result
              {
                 result = operate(j, result, numbers[++i]);
                 //something like this...this does not work correctly
                 //it might give you a hint in the right direction
                 if(result == numbers[arr_len - 1])//compare to last element
                     return 0;
              }  
          }
    }
    return 0;
}
intmain(){
双精度数字[]={3,5,15};//考虑将数字存储为数组
//最后一个元素是结果
双重结果;
int arr_len=sizeof(数字)/sizeof(双精度);
int i,j;
而(1)
{
j=0;
while(j++<5)//覆盖所有运算符
{i=0;
结果=数字[0];//从第一个元素开始
而(i
尝试从
operate()
的主体内部调用
operate()
,并在那里安装一个循环。这将按照您提到的/要求应用递归。将所有输入存储到一个向量中,然后一次迭代向量处理每个元素,可能使用@πάντα建议的递归ῥεῖ不要。使用队列、堆栈或其他东西并对其进行迭代。如果我也想将其应用于其他程序,我想知道如何通过创建一个新的递归函数或更高效的循环来循环这种事情,但不改变其他函数的工作方式。你能告诉我如何做吗?