MT1308 4个自然数

题目

求4个自然数p,q,r,s(p≤q≤r≤s),使得等式1/p+1/q+1/r+1/s=1成立。

格式

输入格式:

输出格式:

输出为整型,空格分隔,每组一行

样例 1

python 复制代码
输入:无
输出:
2 3 7 42
2 3 8 24
2 3 9 18
2 3 10 15
2 3 12 12
2 4 5 20
2 4 6 12
2 4 8 8
2 5 5 10
2 6 6 6
3 3 4 12
3 3 6 6
3 4 4 6
4 4 4 4

程序

python 复制代码
from operator import itemgetter, attrgetter
def main():
    #code here
    lista =[]
    for p in range(1,100):
        for q in range(1,p+1):
            for r in range(1,q+1):
                for s in range(1,r+1):
                    if (1/p+1/q+1/r+1/s==1)and s<=r and r<=q and q<=p:
                        # print("%d %d %d %d"%(s,r,q,p))
                        lista.append((s,r,q,p))
    # 对列表lista中的每个元组升序排列,按照元组的每个元素排序
    lista.sort(key=itemgetter(0,1,2,3))

    for x in lista:
        x = str(x).replace(","," ").replace("(","").replace(")","")
        print(x)
    pass


if __name__ == '__main__':
    main();
相关推荐
databook12 小时前
Manim实现脉冲闪烁特效
后端·python·动效
程序设计实验室12 小时前
2025年了,在 Django 之外,Python Web 框架还能怎么选?
python
倔强青铜三14 小时前
苦练Python第46天:文件写入与上下文管理器
人工智能·python·面试
用户25191624271117 小时前
Python之语言特点
python
刘立军17 小时前
使用pyHugeGraph查询HugeGraph图数据
python·graphql
数据智能老司机21 小时前
精通 Python 设计模式——创建型设计模式
python·设计模式·架构
数据智能老司机1 天前
精通 Python 设计模式——SOLID 原则
python·设计模式·架构
c8i1 天前
django中的FBV 和 CBV
python·django
c8i1 天前
python中的闭包和装饰器
python
这里有鱼汤1 天前
小白必看:QMT里的miniQMT入门教程
后端·python