首页 > 编程语言 > 详细

python算法学习--待续

时间:2019-09-17 00:48:44      阅读:101      评论:0      收藏:0      [点我收藏+]

如果a+b+c = 1000, 且a^2 + b^2 = c^2(a,b,c为自然数),如何求出所有a, b, c可能的组合?

{

a+b+c=1000

a^2 + b^2 = c^2

}

import time
# def condition_solution():
#     start_time = time.time()
#     for a in range(1001):
#         for b in range(1001):
#             for c in range(1001):
#                 if 1000 == a + b + c and a*a + b*b == c*c:
#                     print("a, b, c: %d, %d, %d" % (a, b, c))
#     end_time = time.time()
#     cost = end_time - start_time
#     pri# nt("cost: %f" % cost)
# 
def condition_solution():
    start_time = time.time()
    for a in range(1001):
        for b in range(1001):
                c = 1000 - a - b 
                if 1000 == a + b + c and a*a + b*b == c*c:
                    print("a, b, c: %d, %d, %d" % (a, b, c))
    end_time = time.time()
    cost = end_time - start_time
    print("cost: %f" % cost)

if __name__ == "__main__":
    condition_solution()

 

python算法学习--待续

原文:https://www.cnblogs.com/1314520xh/p/11531076.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!