鱼C论坛

 找回密码
 立即注册
楼主: 冬雪雪冬

[技术交流] Python:每日一题 178

[复制链接]
发表于 2018-6-3 03:44:58 | 显示全部楼层

這個代碼沒有打印出全部的答案阿!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-6-3 16:20:26 | 显示全部楼层
def findxyz(max_num = 100):
    max_z = int(10100**(1/3))
    max_y = int(max_z**0.5)
    max_x = max_num +1
    for x in range(max_x):
        for y in range(max_y):
            for z in range(max_z):
                if x + y**2 == z**3:
                    print(x,y,z)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-6-3 20:24:02 | 显示全部楼层
def findxyz(max_num = 100):
    max_z = int(10100**(1/3))
    max_y = int(max_z**0.5)
    max_x = max_num +1
    for x in range(max_x):
        for y in range(max_y):
            if type((x+y**2)**(1/3)) == int:
                for z in range(max_z):
                    if x + y**2 == z**3:
                        print(x,y,z)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-6-3 20:24:49 | 显示全部楼层
def findxyz(max_num = 100):
    max_z = int(10100**(1/3))
    max_y = int(max_z**0.5)
    max_x = max_num +1
    for x in range(max_x):
        for y in range(max_y):
            if type((x+y**2)**(1/3)) == int:
                for z in range(max_z):
                    if x + y**2 == z**3:
                        print(x,y,z)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-6-4 09:30:39 | 显示全部楼层
x, y 都取最大值时,x+y**2 == 10100, 10100开三次方大约20.xx,由此可见, z的范围不用写到(1,101),只要写到(1,21)就足够了。
所以,x循环100次时 y要循环一百次,总共是10000次, 而每一次循环中,z可以少80次,如此,可减少近80万次计算。。。
  1. import time

  2. start = time.time()
  3. for i in xrange(1, 101):
  4.     for j in xrange(1, 101):
  5.         for k in xrange(1,21):
  6.             if i + j**2 == k**3:
  7.                 print i, j, k

  8. print time.time()-start
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2018-6-4 09:46:02 | 显示全部楼层
  1. res=sorted([(z**3-y**2,y,z) for y in range(1,101) for z in range(1,101) if 1<=z**3-y**2<=100])
  2. for x,y,z in res:
  3.         print(x,y,z)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-6-4 10:04:29 | 显示全部楼层
  1. '''
  2. 求出所以的x, y, z值,使得x + y2 = z3
  3. x, y, z均为1~100的正整数。
  4. 要求输出x, y, z的值,每种可能占一行,例如:
  5. 2 5 3
  6. 4 2 2
  7. 4 11 5
  8. '''
  9. import math
  10. #求z的最大取值
  11. z_max = int((100+100*100)**(1/3))+1
  12. for z in range(1,z_max):
  13.     for y in range(1,101):
  14.         x = z**3 - y**2
  15.         if x < 100 and x > 0:
  16.             print ('%d %d %d ' %(x,y,z))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-6-4 17:03:08 | 显示全部楼层
for x in range(1,101):
    for y in range(1,101):
        for z in range(1,21):
            if x + y**2 == z**3:
                print(x,y,z)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-6-5 13:41:13 | 显示全部楼层
  1. import time

  2. t1 = time.clock()
  3. for x in range(1,101):
  4.         for y in range(1,101):
  5.                 for z in range(1,101):
  6.                         if x + y**2 == z**3:
  7.                                 print(x,y,z)
  8. t2 = time.clock()
  9. print(t2 - t1)

  10. t1 = time.clock()
  11. maxz=int(pow(10100,1/3))  #x+y**2最大值为10100
  12. for z in range(1,maxz+1):
  13.         maxy=int(pow(z**3,1/2))   #y**2的最大值为z**3,此时x为0
  14.         for y in range(1,maxy+1):
  15.                 x=z**3-y*y
  16.                 if x in range(1,101):   #算出x的值需要在1~100之间
  17.                         print(x,y,z)
  18. t2 = time.clock()
  19. print(t2 - t1)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-6-7 01:43:04 | 显示全部楼层
  1. import math
  2. for x in range(1,101):
  3.     for y in range(1,101):
  4.         m = x + y ** 2
  5.         n = int(m ** (1/3))
  6.         for z in range(n,n+2):
  7.             if z ** 3 == m:
  8.                 print(x,y,z)


复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-6-12 14:43:54 | 显示全部楼层
list1 = []
list2 = []
list3 = []
for z in range(1, 101):
    for y in range(1, 101):
        if (z**3 - y**2) - int((z**3 - y**2)) == 0 and (z**3 - y**2)>0 and (z**3 - y**2)<101:
            list1.append(z**3 - y**2)
            list2.append(y)
            list3.append(z)

for x in zip(list1, list2, list3):
    print(x[0], x[1], x[2])

print(len(list1))
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-6-15 10:11:22 | 显示全部楼层
  1. def fun(n):
  2.     lis = [i for i in range(1,n+1)]
  3.     lis1 = []
  4.     for x in lis:
  5.         for y in lis:
  6.             for z in lis:
  7.                 if x + y**2 == z**3:
  8.                     lis2 = [x,y,z]
  9.                     lis1.append(lis2)
  10.     return lis1
  11. n = 100
  12. result = fun(100)
  13. for i in result:
  14.     print('%d %d %d' % (i[0],i[1],i[2]))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-6-15 11:04:03 | 显示全部楼层
for x in range(100):
    for y in range(100):
        for z in range(100):
            if x + y **2 == z**3:
                print(x,'\t',y,'\t',z)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-6-19 16:08:42 | 显示全部楼层
  1. def fun(n):
  2.     lis = [i for i in range(1,n+1)]
  3.     lis1 = []
  4.     for x in lis:
  5.         for y in lis:
  6.             for z in lis:
  7.                 if x + y**2 == z**3:
  8.                     lis2 = [x,y,z]
  9.                     lis1.append(lis2)
  10.     return lis1
  11. n = 100
  12. result = fun(100)
  13. for i in result:
  14.     print('%d %d %d' % (i[0],i[1],i[2]))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-6-20 17:13:20 | 显示全部楼层
shoufei 发表于 2018-6-2 19:03
for x in range(1,100):
    for y in range(1,100):
        for z in range(1,100):

pow效率不如**,**就是快10倍以上!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-7-11 11:27:48 | 显示全部楼层
本帖最后由 duliping 于 2018-7-11 11:36 编辑
  1. x = list(range(1, 101))
  2. # 生成 y 列表
  3. y = []
  4. for i in x:
  5.     y.append(i*i)
  6. # 根据 x 和 y 列表计算出 x + y 的最大值
  7. max = x[-1] + y[-1]
  8. # 生成 z 列表
  9. z = []
  10. for each in zip(x, y):
  11.     item = each[0] * each[1]
  12.     if item > max:
  13.         break
  14.     z.append(item)
  15. # 计算结果
  16. for i in z:
  17.     for j in x:
  18.         diff = i - j
  19.         if diff <= 0:
  20.             break
  21.         if diff in y:
  22.             print(j, y.index(diff)+1, z.index(i)+1)
复制代码


运行结果:
  1. 4 2 2
  2. 7 1 2
  3. 2 5 3
  4. 11 4 3
  5. 18 3 3
  6. 23 2 3
  7. 26 1 3
  8. 15 7 4
  9. 28 6 4
  10. 39 5 4
  11. 48 4 4
  12. 55 3 4
  13. 60 2 4
  14. 63 1 4
  15. 4 11 5
  16. 25 10 5
  17. 44 9 5
  18. 61 8 5
  19. 76 7 5
  20. 89 6 5
  21. 100 5 5
  22. 20 14 6
  23. 47 13 6
  24. 72 12 6
  25. 95 11 6
  26. 19 18 7
  27. 54 17 7
  28. 87 16 7
  29. 28 22 8
  30. 71 21 8
  31. 53 26 9
  32. 39 31 10
  33. 100 30 10
  34. 35 36 11
  35. 47 41 12
  36. 81 46 13
  37. 40 52 14
  38. 11 58 15
  39. 13 70 17
  40. 56 76 18
  41. 79 89 20
  42. 45 96 21
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-7-16 17:54:21 | 显示全部楼层
  1. z3 = [z*z*z for z in range(1, 101)]
  2. y2 = [y*y for y in range(1, 101)]
  3. x1 = [x for x in range(1, 101)]

  4. for i in x1:
  5.     for j in y2:
  6.         if (i+j) in z3:
  7.             print(i, y2.index(j)+1, z3.index(i+j)+1)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-7-16 19:25:32 | 显示全部楼层
本帖最后由 lunagua 于 2018-7-16 19:26 编辑

for x in range(1,101):
        for y in range(1,101):
                for z in range(1,101):
                        if (x+y**2) == z**3:
                                print(x,y,z)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-7-19 23:59:23 | 显示全部楼层
这样不涉及小数,但是嵌套循环免不了
  1. def fun2(n=100):
  2.     result = ""
  3.     for z in range(1, n+1):
  4.         for y in range(1, n+1):
  5.             x = z**3 - y**2
  6.             if 1<= x <= n:
  7.                 result += "{} {} {}\n".format(x, y, z)
  8.     return result
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-2 12:55:10 | 显示全部楼层
本帖最后由 子沙 于 2018-10-2 13:12 编辑
  1. import math
  2. def is_square(num):
  3.     if isinstance(num,int):
  4.         if int(math.sqrt(num))**2==num:
  5.             return True
  6.         
  7. def fun_178(y):
  8.     times=0
  9.     for i in range(2,int((y+y**2)**(1/3))+1):
  10.         temp=int(i**(3/2)) if not is_square(i) else int(i**(3/2))-1
  11.         for j in range(temp,0,-1):
  12.             if i**3-temp**2<=y and i**3-temp**2>0:
  13.                 print(i**3-temp**2,temp,i)
  14.                 temp,times=temp-1,times+1
  15.             else:
  16.                 break
  17.     print('共有%d组符合条件的数'%times)
  18. fun_178(100)
复制代码


结果如下:

  1. 4 2 2
  2. 7 1 2
  3. 2 5 3
  4. 11 4 3
  5. 18 3 3
  6. 23 2 3
  7. 26 1 3
  8. 15 7 4
  9. 28 6 4
  10. 39 5 4
  11. 48 4 4
  12. 55 3 4
  13. 60 2 4
  14. 63 1 4
  15. 4 11 5
  16. 25 10 5
  17. 44 9 5
  18. 61 8 5
  19. 76 7 5
  20. 89 6 5
  21. 100 5 5
  22. 20 14 6
  23. 47 13 6
  24. 72 12 6
  25. 95 11 6
  26. 19 18 7
  27. 54 17 7
  28. 87 16 7
  29. 28 22 8
  30. 71 21 8
  31. 53 26 9
  32. 39 31 10
  33. 100 30 10
  34. 35 36 11
  35. 47 41 12
  36. 81 46 13
  37. 40 52 14
  38. 11 58 15
  39. 13 70 17
  40. 56 76 18
  41. 79 89 20
  42. 45 96 21
  43. 共有42组符合条件的数
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-18 19:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表