此文为找出1-500以内的1勾股数。(且排好序
// 从小到大输出1~500以内的素勾股数组
#include<bits/stdc++.h>
using namespace std;
int a[1234][6], cnt = 1;
int Gcd(int a, int b) {
if (a % b == 0)return b;
return Gcd(b, a % b);
}
int main() {
cout << "1-500以内素勾股" << endl;
for (int i = 1; i <= 30; i++) {
for (int j = i + 1; j <= 54; j++) {
if ((i % 2 != j % 2) && Gcd(j, i) == 1 && (j * j + i * i) <= 500) {
a[cnt][1] = min(j * j - i * i, 2 * i * j);
a[cnt][2] = max(j * j - i * i, 2 * i * j);
a[cnt][3] = j * j + i * i;
cnt++;
}
}
}
for (int i = 1; i < cnt; i++) {
for (int j = i + 1; j <= cnt; j++) {
if (a[i][1] > a[j][1] || (a[i][1] == a[j][1] && a[i][2] > a[j][2])) {
swap(a[i][1], a[j][1]);
swap(a[i][2], a[j][2]);
swap(a[i][3], a[j][3]);
}
}
}
for (int i = 2; i <= cnt; i++)cout << a[i][1] << " " << a[i][2] << " " << a[i][3] << endl;
return 0;
}
)