已知边x,要求其他两条边也是整数,可以先想办法得出未知边中较长边的范围。
1.确定较长边的最大值,较长边是斜边,设为a,另一条边最大为a-1,a^2=(a-1)^2+x^2。
可得出a(max)=sqrt(x^2-1)
2.确定较长边的最小值,已知边为斜边,较长边最小大于等腰直角三角形的直角边。
如图
故a(min)=sqrt(x^2/2)
3.然后勾股定理就行了
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <cmath>
using namespace std;
void triangle(int x)
{
int max = (pow(x, 2) + 1) / 2, min = sqrt(pow(x, 2) / 2);
for (int i = max; i > min; i--)
{
if (i != x && (int)sqrt(abs(pow(i, 2) - pow(x, 2))) * (int)sqrt(abs(pow(i, 2) - pow(x, 2))) == abs(i * i - x * x))
cout << i << "," << sqrt(abs(i * i - x * x)) << endl;
}
cout << endl;
}
int main()
{
int n, m;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> m;
triangle(m);
}
return 0;
}