本原勾股数
題目描述
輸出不少于100100組不同的本原勾股數:?$1≤a≤b≤c≤103$,滿足:$a2+b2=c2$且$gcd(a,b,c)=1$
輸入
無
輸出
根據描述輸出
樣例輸入
無樣例輸出
3 4 5 5 12 13 ....以下省略 你不必輸出完全一樣,輸出不少于100組不同的勾股數,就可以了題解
#include<iostream> #include <set> #include <algorithm> using namespace std; struct GG {int x, y, z;GG(int x, int y, int z) : x(x), y(y), z(z) {}bool operator<(const GG &g) const {if (x != g.x)return x < g.x;if (y != g.y)return y < g.y;return z < g.z;} }; void solve() {set<GG> G;for (int i = 1; i <= 20; i++)for (int j = i + 1; j <= 40; j++) {int x = 2 * i - 1, y = 2 * j - 1;if (__gcd(x, y) == 1) {int a[3] = {(x * x + y * y) / 2, x * y, (y * y - x * x) / 2};sort(a, a + 3);if (a[2] > 1000)continue;if (G.size() < 100)G.insert(GG(a[0], a[1], a[2]));}}for (GG g: G)cout << g.x << " " << g.y << " " << g.z << endl; } int main() {solve();return 0; }相關
總結
- 上一篇: vscode中文
- 下一篇: 使用pg_repack 回收表体积