简体   繁体   English

使用这个应该输出最小可能整数的程序陷入无限循环

[英]Getting caught in an infinite loop with this program that is supposed to output the smallest possible integer

hope someone could help me this and where the error in my code is.希望有人可以帮助我,以及我的代码中的错误在哪里。 I have to write a program for class that does not use arrays or vectors and am restricted in using only nested loops for the exercise.我必须为不使用数组或向量的类编写程序,并且在练习中只能使用嵌套循环。 The program is supposed to run through a series of loops and then outputs the lowest possible integer solution to the problem (a^3 + b^3 = c^3 + d^3).该程序应该运行一系列循环,然后输出问题的最低可能整数解(a^3 + b^3 = c^3 + d^3)。 I'm still learning c++ so please forgive any ignorance or sloppy coding.我仍在学习 C++,所以请原谅任何无知或草率的编码。 Also thanks in advance.也提前致谢。

#include <iostream> 
#include <cmath> 
#include <cstdlib>
#include <cstdio> 


using namespace std; 

int a, b, c, d, n, a_pow, b_pow, c_pow, d_pow, exp_pow, smaller_int,   larger_int, my_num; 


int main()
{


while (n == exp_pow && n == my_num)
{
    for (a = 1; a < 20; a++)
    {
        a_pow = pow(a, 3);
        larger_int = (a_pow + b_pow);

        for (b = 1; b < 20; b++)
        {
            b_pow = pow(b, 3);
            smaller_int = (a_pow + b_pow);
            if (larger_int < smaller_int)
            {
                smaller_int = larger_int;
            } 
                for (c = 1; c < 20; c++)
                {
                    c_pow = pow(c, 3); 
                    larger_int = (c_pow + d_pow);
                    if (larger_int < smaller_int)
                    {
                        smaller_int = larger_int;
                    }
                        for (d = 1; d < 20; d++)
                        {
                            d_pow = pow(d, 3); 
                            larger_int = (c_pow + d_pow);
                            if (larger_int < smaller_int)
                            {
                                smaller_int = larger_int;
                                exp_pow = smaller_int; 
                            }
                        }

                }

        }
    }

}

cout << smaller_int << endl;    
return 0;
} 

Edit: I think I pretty much got it to work, not sure if the computation is right, but was able to produce an output from the code.编辑:我想我几乎让它工作了,不确定计算是否正确,但能够从代码中产生输出。 Thank you to the ones that did provide some insight.感谢那些确实提供了一些见解的人。 Yes I was not updating the variables correctly while inside the loop.是的,我在循环内没有正确更新变量。 Now I just have to check my logic.现在我只需要检查我的逻辑。

On a side note, down voted for asking a question???在旁注中,否决了提出问题的投票??? Smh.嗯。

If you want to get technical, the while loop is what never ends.如果你想获得技术,while 循环是永无止境的。

It's because you never assign a value to a, b, c, or d before you do:这是因为你从来没有在你这样做之前给 a、b、c 或 d 赋值:

a_pow = pow(a, 3); 
b_pow = pow(b, 3); 
c_pow = pow(c, 3);
d_pow = pow(d, 3);

So a,b,c,d are all 0, and thus a_pow:d_pow are all also 0. So when you do this:所以 a,b,c,d 都是 0,因此 a_pow:d_pow 也是 0。所以当你这样做时:

n = (a_pow + b_pow);   
exp_pow = (c_pow + d_pow) 

(and by the way you need a semi-colon at the end of the 2nd line there), both n and exp_pow = 0. (顺便说一下,您需要在第 2 行的末尾加一个分号),n 和 exp_pow = 0。

Since your loop never reassigns values to either n or ex_pow, this:由于您的循环永远不会将值重新分配给 n 或 ex_pow,因此:

while (n == exp_pow)

is always true.永远是真的。 I should add that there are a lot of other problems in the code, as well.我应该补充一点,代码中还有很多其他问题。 But this does answer your actual question.但这确实回答了您的实际问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM