只要l是1,h就可以是任何數字,此循環就可以工作。但我需要它從不同的範圍工作,如l = 20 h = 40?誰能告訴我該怎麼做?我將不勝感激。不能製作嵌套循環
#include <iostream>
#include <vector>
#include <list>
#include <math.h>
#include <algorithm>
using namespace std;
int main(void)
{
int a, b, c, i = 0;
unsigned long l = 1;
unsigned long h = 25;
int array[3];
for (a = l; l <= a && a <= h; ++a)
for (b = a; l <= b && b <= h; ++b)
for (c = b; l < c && c <= h; ++c)
if (a * a + b * b == c * c)
{
array[0] = a;
array[1] = b;
array[2] = c;
if (array[0]+array[1]+array[2] <= h)
cout << array[0] << " " << array[1] << " " << array[2] <<endl;
else
break;
}
return 0;
}
縮進你的代碼。你不會後悔的。 – Blender 2012-03-10 03:56:34
我看到的第一件事情是錯誤的(除了縮進),你是愉快地將'unsigned long'轉換爲'int'而不需要進一步提問。這可能不是問題,但這仍是一個問題。 – netcoder 2012-03-10 04:01:12
@ user1219733,如果您在解決問題的堆棧溢出中得到答案,請接受它們爲「接受」。這是通過點擊答案分數下的複選標記完成的。 – Shahbaz 2012-03-17 19:41:51