项目Euler Number Nine with C ++

时间:2012-07-25 04:51:57

标签: c++

我写了这个,但是当它运行时,控制台只是坐在“正在运行......”并且不会真正做任何事情,至少我可以看到。我在这里不知所措,因为我想不出别的事情。

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

int main(void) {
    int count = 0;
    do {

        int a = 1;
        int b = 2;
        int c = 3;
        int total;

        for (a=1;a<b;a++) {
            for (b=2;b<c;b++) {
                for (c=3;c<=1000;c++) {
                    total = a+b+c;

                    if (total == 1000 && a*a + b*b == c*c) {
                        std::cout << a << ", " << b << ", " << c;

                    }
                }
            }
        }



        count++;

    } while(count < 1000);  
    return 0;
    std::cin.get();
}

1 个答案:

答案 0 :(得分:1)

您可能看不到任何输出,因为您的终端是行缓冲的,并且您永远不会写入换行符或刷新流。要解决此问题,您可以将std::endl添加到输出行:

std::cout << a << ", " << b << ", " << c << std::endl;

通过这种方式,您应该在找到所有三元组后立即查看,但该程序仍需要很长时间才能完成。甚至可能需要很长时间才能找到任何结果。您可以通过避免一些嵌套循环来加速程序。