我的程序應該列出1-500之間的所有直角三角形三元組。它不應該重複相同的三角形。例如3,4,5與4,3,5相同,只顯示第一個。我也應該在計劃結束時有一個計數器,顯示找到了多少個三角形。到目前爲止,這是我的。它目前沒有顯示正確數量的三角形,並且計數器工作不正常。由於我的三角形三元組程序有個問題
// Naming
int counter;
// For loops and nested for loops
{
// Makes sure side A starts at 1 and is less than 500
for (int a = 1; a <= 500; a++)
{
// Makes sure side B starts at 1 and is less than 500
for (int b = 1; b <= 500; b++)
{
// Makes sure side C starts at 1 and us kess than 500
for (int c = 1; c <= 500; c++)
{
// If A squared + B squared = C squared and C squared, A, and B -->
// are all less than or equal to 500 then display the answer
if ((a*a)+(b*b) == c*c & a & b <= 500) {
// This is my counter I cannot seem to get it to work properly
// More info about counter at bottom
counter++;
cout << a << ", " << b << ", " << c << endl;
}
}
}
}
}
cout << endl;
// Displaying counter
cout << counter << endl << endl ;
system("PAUSE");
return EXIT_SUCCESS;
}
循環過多:如果您知道'a'和'b',則可以計算'c'。 – 2014-10-09 03:23:46