我的程序应该列出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