生成右三角形C++

生成右三角形C++,c++,C++,我试图做一个程序,将生成3边给定以下输入:允许的最长斜边和所需的三角形数。边只能是整数。 我写的程序挂起,不返回任何输出。 如果你要否决我,请解释原因 #include <iostream> #include <cmath> #include <cstdlib> int generator(int number, int hypoth){ int a,b,c; while (number>0){ c=rand()%(

我试图做一个程序,将生成3边给定以下输入:允许的最长斜边和所需的三角形数。边只能是整数。 我写的程序挂起,不返回任何输出。 如果你要否决我,请解释原因

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

int generator(int number, int hypoth){

    int a,b,c;

    while (number>0){
        c=rand()%(hypoth-1)+1;
        for (a=1;a<hypoth-2;a++){
            for (b=1;pow(a,2)+pow(b,2)<=pow(c,2); b++){
                if (pow(a,2)+pow(b,2)==pow(c,2)){
                    std::cout<<"sides: "<<a<<" "<<b<<" "<<c<<std::endl;
                    number--;
                }
            }
        }
    }
    return 0;
}

int main(){
    int triangle_number, hypothenuse;
    std::cout << "How many triangles to generate? ";
    std::cin >> triangle_number;
    std::cout << "How long is max hypothenuse?";
    std::cin >> hypothenuse;
    generator(triangle_number, hypothenuse);
    return 0;
} 
如果你认为我应该改进我的算法,请告诉我正确的方向。
谢谢您的时间。

您提供的代码在我的机器上运行良好:输入1和6给出输出端:3 4 5

然而,问题可能来自于这条线:powa,2+powb,2==powc,2。战俘返回双打。比较浮点数是否相等是一件棘手的事情,而且几乎从来都不是一个好主意,因为它可能会偏离一个很小的数值,并且是错误的


将其替换为a*a+b*b==c*c,将上面for循环中的条件替换为a*a+b*b欢迎使用堆栈溢出!听起来您可能需要学习如何使用调试器逐步完成代码。有了一个好的调试器,您可以逐行执行您的程序,并查看它偏离预期的地方。这是一个必要的工具,如果你要做任何编程。进一步阅读:你使用了什么输入?也许只是时间太长了finish@tobi303输入1,6 1三角形,最长斜边6导致悬挂。此外,如果它能找到答案,它会立即发布,因此我怀疑情况是否如此;因为它可能需要一段时间,直到它猜出一个足够大的数字,这是以前没有猜到的。