转载的老师发的答案,关注我,随时更新答案,如果来不及了,可以私信我,我发给你们相应的答案
#include <iostream>
#include <iomanip>
using namespace std;
bool isPythagoreanTriple(int, int, int);
int main()
{
int counter{0};
cout << "All Pythagorean Triples up to sides of 500" << endl;
cout << "Number" << setw(9) << "Side1 " << setw(9) << "Side2" << setw(9) << "Side3" << endl;
for (int i = 1;i <= 500;++i)
{
for (int j = i + 1;j <= 500;++j)
{
for (int k = j + 1;k <= 500;++k)
{
if (isPythagoreanTriple(i, j, k))
{
++counter;
cout << setw(3) << counter << setw(9) << i << setw(9) << j << setw(9) << k << endl;
}
}
}
}
cout << endl << endl << "Total " << counter << " Pythagorean Triples found!!" << endl;
return 0;
}
bool isPythagoreanTriple(int a, int b, int c)
{
int hypotenuse = max(a, std::max(b, c));
if (hypotenuse == a)
{
return (b * b + c * c == hypotenuse * hypotenuse);
}
else if (hypotenuse == b)
{
return (a * a + c * c == hypotenuse * hypotenuse);
}
else
{
return (a * a + b * b == hypotenuse * hypotenuse);
}
}
下面是运行结果:
|