鱼C论坛

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

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

[复制链接]
发表于 2018-9-11 22:21:04 | 显示全部楼层
本帖最后由 子沙 于 2018-9-11 22:22 编辑
  1. def fun_207(num):
  2.     x1,x2=1,1
  3.     x1,x2=x2,x1+x2
  4.     num1=0
  5.     while num1<num:
  6.         x1,x2=x2,x1+x2
  7.         if len(str(x2))>1:
  8.             flag=0
  9.             for i in sorted(set(list(map(lambda x:int(x),list(str(x2))))),reverse=True):
  10.                 if i!=0:
  11.                     if x2%i!=0:
  12.                         flag=1
  13.                         break
  14.             if flag==0:
  15.                 print(x2)
  16.                 num1+=1
  17. fun_207(10)
复制代码


运行结果如下:

  1. 55

  2. 144

  3. 46368

  4. 5358359254990966640871840

  5. 64202014863723094126901777428873111802307548623680

  6. 769246427201094785080787978422393713094534885688979999504447628313150135520

  7. 9216845717656874712980450562726202415567360565980794777111390850331644813674856981646960226192287360

  8. 110433070572952242346432246767718285942590237357555606380008891875277701705731473925618404421867819924194229142447517901959200

  9. 1323171012053243520828784042795469593341319770463238313551473338336502410952765153371119398122747569819754164672344667591018783803781288766524146031040

  10. 15853779289614481577191691577533518174485949824765525957623037737532160833631593681512535189759649213804709691705593604227037581022758330489263385511067756715301085561591382880
复制代码

评分

参与人数 1荣誉 +2 鱼币 +2 收起 理由
冬雪雪冬 + 2 + 2

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-11 22:28:09 | 显示全部楼层
本帖最后由 瓦蓝 于 2018-9-11 22:37 编辑
  1. a,b=5,8
  2. def fab():
  3.     global a,b
  4.     a,b=b,a+b
  5.     c=b%2520
  6.     n=set(str(b))
  7.     try:
  8.         n.remove('0')
  9.         n.remove('1')
  10.     except:pass
  11.     for i in n:
  12.         if c%int(i):fab()
  13.     print(b)
  14.     fab()
  15. fab()
复制代码

评分

参与人数 1荣誉 +1 鱼币 +1 收起 理由
冬雪雪冬 + 1 + 1 递归溢出了

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-11 22:39:31 | 显示全部楼层
  1. def sum(n):
  2.     p = {}
  3.     s = 0
  4.     temp = n
  5.     while n:
  6.         p[n % 10] = 1
  7.         n //= 10
  8.     if p.get(9) and p.get(3):
  9.         p.pop(3)
  10.     if p.get(8):
  11.         if p.get(4):
  12.             p.pop(4)
  13.         if p.get(2):
  14.             p.pop(2)
  15.     if p.get(4) and p.get(2):
  16.         p.pop(2)
  17.     if p.get(6):
  18.         if p.get(3):
  19.             p.pop(3)
  20.         if p.get(2):
  21.             p.pop(2)
  22.     if p.get(0):
  23.         p.pop(0)
  24.     flag = 1
  25.     for each in p:
  26.         if temp % each != 0:
  27.             flag = 0
  28.             break;
  29.     return flag
  30. a = [5,8]
  31. for i in range(2,900):
  32.     t = a[i-1] + a[i-2]
  33.     a.append(t)
  34. cnt = 0
  35. for each in a:
  36.     if cnt == 10:
  37.         break;
  38.     if each > 8 and sum(each):
  39.         cnt += 1
  40.         print(each)
复制代码


55

144

46368

5358359254990966640871840

64202014863723094126901777428873111802307548623680

769246427201094785080787978422393713094534885688979999504447628313150135520

9216845717656874712980450562726202415567360565980794777111390850331644813674856981646960226192287360

110433070572952242346432246767718285942590237357555606380008891875277701705731473925618404421867819924194229142447517901959200

1323171012053243520828784042795469593341319770463238313551473338336502410952765153371119398122747569819754164672344667591018783803781288766524146031040

15853779289614481577191691577533518174485949824765525957623037737532160833631593681512535189759649213804709691705593604227037581022758330489263385511067756715301085561591382880

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-11 22:41:43 | 显示全部楼层
  1. def aliquot(num):
  2.         if '0' in str(num):
  3.                return False
  4.         else:
  5.             for i in range(len(str(num))):
  6.                   if num%int(str(num)[i]) == 0:
  7.                        return True
  8.                   else:
  9.                        return False

  10. def fibsNum():
  11.         a = 0
  12.         b = 1
  13.         num_list = []
  14.         while True:
  15.                 a,b = b,a+b
  16.                 if a > 10:
  17.                        zc_status = aliquot(a)
  18.                        if zc_status is True:
  19.                                 num_list.append(a)
  20.                                 if len(num_list) == 10:
  21.                                         return num_list




  22. print(fibsNum())
复制代码


# [13, 55, 144, 1597, 2584, 17711, 46368, 121393, 196418, 317811]
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-11 23:18:28 | 显示全部楼层
  1. >>> def maxYs( x, y ):
  2.         x,y = (x,y) if x < y else (y,x)
  3.         while x:
  4.                 x,y = y%x, x
  5.         return y

  6. >>> def minBs( *ns ):
  7.         r = 1
  8.         for n in ns:
  9.                 if n:
  10.                         r = r * n // maxYs(r,n)
  11.         return r

  12. >>> minBs(4,6)
  13. 12
  14. >>> def fb():
  15.         a,b=1,1
  16.         while True:
  17.                 yield a
  18.                 a,b = b,a+b

  19.                
  20. >>> def fun( n = 10 ):
  21.         res = []
  22.         f = fb()
  23.         cnt = 0
  24.         while cnt < n:
  25.                 t = next(f)
  26.                 if t < 10:
  27.                         continue
  28.                 if t % minBs(*[int(x) for x in str(t)]) == 0:
  29.                         cnt += 1
  30.                         res.append( t )
  31.         return res

  32. >>> fun()
  33. [55, 144, 46368, 5358359254990966640871840, 64202014863723094126901777428873111802307548623680, 769246427201094785080787978422393713094534885688979999504447628313150135520, 9216845717656874712980450562726202415567360565980794777111390850331644813674856981646960226192287360, 110433070572952242346432246767718285942590237357555606380008891875277701705731473925618404421867819924194229142447517901959200, 1323171012053243520828784042795469593341319770463238313551473338336502410952765153371119398122747569819754164672344667591018783803781288766524146031040, 15853779289614481577191691577533518174485949824765525957623037737532160833631593681512535189759649213804709691705593604227037581022758330489263385511067756715301085561591382880]
  34. >>>
复制代码

评分

参与人数 1荣誉 +2 鱼币 +2 收起 理由
冬雪雪冬 + 2 + 2

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-11 23:41:51 | 显示全部楼层

  1. a,b,n=1,1,0

  2. while n<10:
  3.     count = 1
  4.     b,a=a+b,b
  5.     for x in set(str(b)):
  6.         if int(x) != 0:
  7.             if b%int(x) != 0:
  8.                 count = 0
  9.     if b > 10 and count:
  10.         n += 1
  11.         print(n,b)
复制代码

评分

参与人数 1荣誉 +2 鱼币 +2 收起 理由
冬雪雪冬 + 2 + 2

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-12 10:40:15 | 显示全部楼层
def check(n):
        res=[0 for i in range(10)]
        for x in map(int,set(str(n))):
                res[x]=1
        if res[9]==1:
                res[3]=0
        if res[8]==1:
                res[4]=0
                res[2]=0
        if res[6]==1:
                res[3]=0
                res[2]=0
        if res[4]==1:               
                res[2]=0       
        for x in [i for i in range(2,10) if res[i]==1]:
                if n%x>0:
                        return False
        return True
       



a,b,n=5,8,0
while n<10:
        b,a=a+b,b               
        if check(b):
                n+=1
                print(n,b)

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-12 10:57:12 | 显示全部楼层
def fib():
    a,b=8,13
    while True:
        a,b=b,a+b
        d = str(a)          #将整型数转化为字符
        c = map(int,d)      #将字符串的每一位取出来转化未整型,再返回一个列表
        f = map(int,d)      #将字符串的每一位取出来转化未字符,再返回一个列表
        m = set(f)
        e = list(m)
        #print(e)
        length = len(e)
        #print(length)
        j = 0
        for i in range(length):
            if e[i]==0:
                continue
            elif a%e[i]==0:   
                j +=1
            else:
                break
        if j==length:
            yield a

            
a=fib()
for i in range(10):
    print(next(a))

评分

参与人数 1荣誉 +1 鱼币 +1 收起 理由
冬雪雪冬 + 1 + 1 等了半天才出来3个数

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-12 11:59:32 | 显示全部楼层
  1. def a207(n):
  2.     a=5
  3.     b=8
  4.     while n:
  5.         c=a+b
  6.         s3=str(c)
  7.         if '0' in s3:
  8.             s3=s3.replace('0','')
  9.         if '1' in s3:
  10.             s3=s3.replace('1','')        
  11.         s2=list(set(([int(i) for i in s3])))
  12.         if 6 in s2:
  13.             try:
  14.                 s2.remove(3)
  15.                 s2.remove(2)
  16.             except ValueError:
  17.                 pass
  18.         if 8 in s2:
  19.             try:
  20.                 s2.remove(4)
  21.                 s2.remove(2)
  22.             except ValueError:
  23.                 pass
  24.         if 9 in s2:
  25.             try:
  26.                 s2.remove(3)
  27.             except ValueError:
  28.                 pass
  29.         if 4 in s2:
  30.             try:
  31.                 s2.remove(2)
  32.             except ValueError:
  33.                 pass
  34.         count=0
  35.         for i in s2:
  36.             if c%i:
  37.                 count+=1
  38.         if count==0:
  39.             print(c)
  40.             n-=1
  41.             a=b
  42.             b=c
  43.         else:
  44.             a=b
  45.             b=c
  46.             count=0
  47.         
  48. a207(10)
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

头像被屏蔽
发表于 2018-9-12 11:59:34 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-12 13:47:38 | 显示全部楼层
本帖最后由 FC的注册很坑 于 2018-9-12 14:36 编辑
  1. #再来个超长版本
  2. def fib(n):
  3.     a=0
  4.     b=1
  5.     for i in range(n):
  6.         a,b=b,a+b
  7.     return b

  8. def fun207(n):
  9.     st=str(n)
  10.     li=paixu(st)
  11.     a0=False
  12.     a1=False
  13.     a2=False
  14.     a3=False
  15.     a4=False
  16.     for i in li:
  17.         if i==0:
  18.             a0=True
  19.         if i==1:
  20.             a1=True
  21.         if i==4 or i==6 or i==8:
  22.             a2=True
  23.         if i==6 or i==9:
  24.             a3=True
  25.         if i==8:
  26.             a4=True
  27.     if a0==True:
  28.         li.remove(0)
  29.     if a1==True:
  30.         li.remove(1)
  31.     if a2==True:
  32.         try:
  33.             li.remove(2)
  34.         except:
  35.             pass
  36.     if a3==True:
  37.         try:
  38.             li.remove(3)
  39.         except:
  40.             pass
  41.     if a4==True:
  42.         try:
  43.             li.remove(4)
  44.         except:
  45.             pass
  46.     for i in li:
  47.         if n % i!=0:
  48.             return False
  49.     return True

  50. def paixu(st):  # 去重和由大到小排序
  51.     l=len(st)
  52.     a=[]
  53.     for i in range(l):
  54.         a.append(int(st[i]))
  55.     a=list(set(a))
  56.     a.sort()
  57.     a.reverse()
  58.     return a

  59. count=7
  60. total=1
  61. while total<=10:
  62.     a=fib(count)
  63.     count+=1
  64.     if fun207(a):
  65.         print(a)
  66.         total+=1
复制代码
先贴个初稿上来看看情况
  1. def fib(n):
  2.     a=0
  3.     b=1
  4.     for i in range(n):
  5.         a,b=b,a+b
  6.     return b

  7. def fun207(n):
  8.     st=str(n)
  9.     list=paixu(st)
  10.     for i in list:
  11.         if i!=0 and i!=1: # 不用除以0和1
  12.             if n % i!=0:
  13.                 return False
  14.     return True

  15. def paixu(st):
  16.     l=len(st)
  17.     a=[]
  18.     for i in range(l):
  19.         a.append(int(st[i]))
  20.     a=list(set(a))
  21.     a.sort()
  22.     a.reverse()
  23.     return a

  24. count=7
  25. total=1
  26. while total<=10:
  27.     a=fib(count)
  28.     count+=1
  29.     if fun207(a):
  30.         print(a,end=" ")
  31.         total+=1
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-12 14:00:27 | 显示全部楼层
昨天的今天可以给看答案了吧!!!!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-12 14:44:07 | 显示全部楼层
本帖最后由 raphael213 于 2018-9-12 16:06 编辑

a, b, i = 0, 1, 0
list = []
while(i < 10):
    a, b = b, a+b
    str_a = str(a)
    str_a1 = ""
    n = 0
    if a >10:
#去重,和0
        for x in str_a:
            if int(x) != 0:
                if x not in str_a1:
                    str_a1 += x

        for each in str_a1:
            if a % int(each) == 0:
                n += 1
        if n == len(str_a1):
            list.append(a)
            i += 1

for i in range(len(list)):
    print(list[i])
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-12 15:53:58 | 显示全部楼层
  1. def fab():
  2.     a, b = 5, 8
  3.     while True:
  4.         a, b = b, a + b
  5.         yield b

  6. def main():
  7.     num = fab()
  8.     count = 0
  9.     while count < 10:
  10.         next_num = next(num)
  11.         next_num_list = list(set([int(i) for i in str(next_num)]))
  12.         if 1 in next_num_list:
  13.             next_num_list.remove(1)
  14.         if 0 in next_num_list:
  15.             next_num_list.remove(0)
  16.         primes = [2, 3, 5, 7]
  17.         next_num_list.sort(reverse=True)
  18.         new_next_num_list = next_num_list.copy()
  19.         for i in range(len(next_num_list)-1):
  20.             if next_num_list[i] in primes:
  21.                 continue
  22.             else:
  23.                 for j in range(i+1, len(next_num_list)):
  24.                     if next_num_list[i] % next_num_list[j] == 0 and next_num_list[j] in new_next_num_list:
  25.                         new_next_num_list.remove(next_num_list[j])
  26.         result_list = []
  27.         for each in new_next_num_list:
  28.             if next_num % each:
  29.                 result_list.append(False)
  30.             else:
  31.                 result_list.append(True)
  32.         if False not in result_list:
  33.             print(next_num)
  34.             count += 1




  35. if __name__ == '__main__':
  36.     main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-12 15:55:38 | 显示全部楼层
本帖最后由 ccqzhm 于 2018-9-12 17:04 编辑
  1. empty_set = set()
  2. repeat_dict = {9:{3}, 8:{4,2}, 7:empty_set, 6:{3, 2}, 5:empty_set, 4:{2}, 3:empty_set, 2:empty_set}
  3. def check(n):
  4.     if n < 10:
  5.         return False

  6.     number_set = set([int(x) for x in str(n) if x > '1'])

  7.     remov_set = set()
  8.     for x in number_set:
  9.         remov_set |= repeat_dict[x]
  10.     number_set -= remov_set

  11.     # print(n, str(number_set))
  12.     for x in number_set:
  13.         if n % x != 0:
  14.             return False
  15.     return True


  16. def fib():
  17.     a,b = 0,1
  18.     while True:
  19.         a, b = b, a + b
  20.         if check(b):
  21.             yield b

  22. def fun207():
  23.     f = fib()
  24.     for i in range(10):
  25.         print(next(f))

  26. fun207()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-12 15:56:22 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-9-12 20:30:20 | 显示全部楼层
天圆突破 发表于 2018-9-11 17:01
我觉得考虑效率的话,去重去01外加逆排序就足够了
如果为了去掉2,3,4(只能去这3个数字),需要遍历4次才能 ...

万一这个数恰好就是0,1组成的呢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-12 20:36:09 | 显示全部楼层
  1. def fun207(temp):
  2.     tempS = temp
  3.     while tempS:
  4.         num = tempS%10
  5.         tempS //= 10
  6.         if num == 0:
  7.             continue
  8.         elif temp%num == 0:
  9.             continue
  10.         else:
  11.             return 0
  12.             break
  13.     return 1
  14.             
  15. def fab(numMax):
  16.     each,a,b = 0,8,13
  17.     flag = 0
  18.     while each<numMax:
  19.         temp = b
  20.         flag = fun207(temp)
  21.         if flag == 1:
  22.             yield b
  23.             a,b = b,a+b
  24.             each += 1
  25.         else:
  26.             a,b = b,a+b

  27. for each in fab(10):
  28.     print('-->',each)
  29.             
复制代码

评分

参与人数 1荣誉 +2 鱼币 +2 收起 理由
冬雪雪冬 + 2 + 2

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-9-12 21:08:11 | 显示全部楼层
塔利班 发表于 2018-9-12 20:30
万一这个数恰好就是0,1组成的呢

有道理,虽然没有碰到这样的数,但也应该考虑到。我在出题时没有想周全。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-9-13 10:08:48 | 显示全部楼层
  1. def func207(n, c):
  2.     #n表示需要输出的数字最小值屏蔽,屏蔽个位数,c表示需要多少个这样的数
  3.     tmp = []
  4.     for i in fab():
  5.         if n <= i:
  6.             tmplist=cushu(i)
  7.             if tmplist:
  8.                 for a in tmplist:
  9.                     if i % a != 0:
  10.                         break
  11.                 else:
  12.                     tmp.append(i)
  13.         if len(tmp) >= c:
  14.             return tmp

  15. def cushu(l):
  16.     tmplist = set()
  17.     for j in str(l):
  18.         if int(j) % 5 == 0:
  19.             tmplist.add(5)
  20.         if int(j) % 7 == 0:
  21.             tmplist.add(7)
  22.         if int(j) % 2 == 0:
  23.             tmplist.add(2)
  24.         if int(j) % 3 == 0:
  25.             tmplist.add(3)
  26.     if tmplist:
  27.         return tmplist
  28.     else:
  29.         return
  30. def fab():
  31.     n, a, b = 0, 0, 1
  32.     while 1:
  33.         yield b
  34.         a, b = b, a + b
  35.         n = n + 1
  36. import time
  37. start = time.time()
  38. print(func207(10, 10))
  39. print(time.time()-start)
复制代码

使用生成器的写法,yield的写法,不知道符不符合效率要求。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-25 18:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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