• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • YouClaw
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

RomanLin

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

【勾股定理】牛客周赛 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';
        }
    }
}

posted on 2026-04-19 21:42  RomanLin  阅读(5)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3