【勾股定理】牛客周赛 Round 140 F. 小红的三角形构造
题目
https://ac.nowcoder.com/acm/contest/132940/F
题解
构造勾股数的结论:奇数平方取一半,偶数半方加减一。
参考代码
#include<bits/stdc++.h>
using namespace std;
int main() {
int T;
cin >> T;
while (T --) {
int x; cin >> x;
if (x <= 2) {// 小于 3 的情况无解
cout << "No\n";
continue;
}
cout << "Yes\n";
if (x & 1) {// 奇数平方取一半
long long y = 1LL * x * x;
cout << x << " " << y / 2LL << " " << y / 2LL + 1LL << '\n';
} else {// 偶数半方加减一
long long y = (x / 2LL) * (x / 2LL);
cout << x << " " << y + 1LL << " " << y - 1LL << '\n';
}
}
}
浙公网安备 33010602011771号