蓝桥杯刷题-四平方和

四平方和

代码:

python 复制代码
from copy import deepcopy
n = int(input())
maxn = int(5e6) + 10
dic = dict()
for a in range(maxn):
    if a * a > n:
        break
    for b in range(a,maxn):
        if a * a + b * b > n:
            break
        if dic.get(a*a+b*b) is None:
            dic[a*a+b*b] = (a,b)
ans = [maxn for _ in range(4)]
for a in sorted(dic.keys()):
    b = n - a
    if a > b:
        break
    if b in dic.keys():
        tmp = list(dic[a]+dic[b])
        tmp.sort()
        for i in range(4):
            if tmp[i] != ans[i]:
                if tmp[i] < ans[i]:
                    ans = deepcopy(tmp)
                break
for i in range(3):
    print(ans[i], end = ' ')
print(ans[-1])

作者:波尔k
链接:https://www.acwing.com/activity/content/code/content/8139085/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
相关推荐
databook9 小时前
Manim实现脉冲闪烁特效
后端·python·动效
程序设计实验室9 小时前
2025年了,在 Django 之外,Python Web 框架还能怎么选?
python
倔强青铜三11 小时前
苦练Python第46天:文件写入与上下文管理器
人工智能·python·面试
用户25191624271114 小时前
Python之语言特点
python
刘立军14 小时前
使用pyHugeGraph查询HugeGraph图数据
python·graphql
聚客AI16 小时前
🙋‍♀️Transformer训练与推理全流程:从输入处理到输出生成
人工智能·算法·llm
数据智能老司机18 小时前
精通 Python 设计模式——创建型设计模式
python·设计模式·架构
大怪v19 小时前
前端:人工智能?我也会啊!来个花活,😎😎😎“自动驾驶”整起!
前端·javascript·算法
数据智能老司机19 小时前
精通 Python 设计模式——SOLID 原则
python·设计模式·架构
c8i20 小时前
django中的FBV 和 CBV
python·django