|
发表于 2018-6-1 15:59:46
|
显示全部楼层
程序执行结果:
- 7 1 2
- 4 2 2
- 26 1 3
- 23 2 3
- 18 3 3
- 11 4 3
- 2 5 3
- 63 1 4
- 60 2 4
- 55 3 4
- 48 4 4
- 39 5 4
- 28 6 4
- 15 7 4
- 89 6 5
- 76 7 5
- 61 8 5
- 44 9 5
- 25 10 5
- 4 11 5
- 95 11 6
- 72 12 6
- 47 13 6
- 20 14 6
- 87 16 7
- 54 17 7
- 19 18 7
- 71 21 8
- 28 22 8
- 53 26 9
- 39 31 10
- 35 36 11
- 47 41 12
- 81 46 13
- 40 52 14
- 11 58 15
- 13 70 17
- 56 76 18
- 79 89 20
- 45 96 21
- 程序执行了0.178196s。
复制代码
程序代码:
- """
- 求出所以的x, y, z值,使得x + y2 = z3
- x, y, z均为1~100的正整数。
- 要求输出x, y, z的值,每种可能占一行,例如:
- 2 5 3
- 4 2 2
- """
- import time
- def square(start_number, end_number):
- """
- 求出范围内所有平方数,以列表返回
- """
- result = []
- for each in range(start_number, end_number):
- sq = each * each
- if sq <= end_number:
- result.append([each, sq])
- else:
- break
- return result
- def cubed(start_number, end_number):
- """
- 求出范围内所有立方数,以列表返回
- """
- result = []
- for each in range(start_number, end_number):
- cube = each * each * each
- if cube <= end_number:
- result.append([each, cube])
- else:
- break
- return result
- def main():
- squ = square(1, 100 * 100)
- cube = cubed(1, 100 * 100 * 100)
- for z3 in cube:
- for y2 in squ:
- for x in range(1, 100):
- if x + y2[1] == z3[1]:
- print(x, y2[0], z3[0])
- if x >= z3[1]:
- break
- if y2[1] >= z3[1]:
- break
- if __name__ == '__main__':
- start = time.clock()
- main()
- end = time.clock()
- print("程序执行了%fs。" % (end - start))
复制代码
|
评分
-
查看全部评分
|