如何根据需要创建尽可能多的嵌套循环? [英] How to create as many nested loops as you want?

查看:87
本文介绍了如何根据需要创建尽可能多的嵌套循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个程序,该程序可以用C ++语言来接受许多数字. 然后,它找到哪些运算符可以使方程式成立,并显示所有正确的可能运算. 示例:如果我放3 5 15 然后输出3x5 = 15 如果我放1 2 3 4 4 然后输出1 + 2-3 + 4 = 4

I am trying to create a program that can take many numbers as I want in C++ language. Then it find what operators can make the equation true and show all correct possible operation. Example: If I put 3 5 15 Then it output 3x5 = 15 If I put 1 2 3 4 4 Then it outputs 1+2-3+4 =4

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

The following code is my written program: The problem about it is that when I want to reduce the number of input or increase the number of input I need to add/reduce nested loops EVERYTIME. I want to know what is a more effective way of a more flexible nested loops or recursion method.

#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 ' ';
}

推荐答案

您可能想使用while()循环,因为您不知道循环何时终止.

You might want to use while() loops, cause you dont know when the loop terminates.

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;
}

这篇关于如何根据需要创建尽可能多的嵌套循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆