Home > Backend Development > Python Tutorial > python求解水仙花数的方法

python求解水仙花数的方法

WBOY
Release: 2016-06-10 15:12:50
Original
1840 people have browsed it

本文实例讲述了python求解水仙花数的方法。分享给大家供大家参考。具体如下:

一个N位的十进制正整数,如果它的每个位上的数字的N次方的和等于这个数本身,则称其为花朵数。

#!/usr/bin/python
def get_flower(n, ofile):
 D_pow=[pow(i,n) for i in range(0,10)]
 V_min=1*pow(10,n-1)
 V_max=sum((9*pow(10,x) for x in range(0,n)))
 T_count=0
 print D_pow, V_max, V_min
 nums=[1]+[0]*(n-1)
 print 'Start:', nums
 idx=n-1
 tmp_l=[0]*10
 while True:
  nums[idx]+=1
  if nums[idx]<10:
   j=idx+1
   while j<n:
    nums[j]=nums[idx] # reset 
    j+=1
   v=sum((D_pow[x] for x in nums))
   if v<=V_max and v>=V_min:
    T_count+=1
    #test if is flower
    #print 'do test:', ''.join(map(str,nums))
    k=0
    while k<10:
     tmp_l[k]=0
     k+=1
    N=0
    for k in nums:
     tmp_l[k]+=1
     N+=1
    while N>0:
     p=v%10
     if tmp_l[p]>0:
      tmp_l[p]-=1
      N-=1
     else:
      break
     v/=10
    if N==0:
     print >>ofile, 'hit', sum((D_pow[x] for x in nums))
   idx=n-1
  elif idx==0:
   print 'done'
   break
  else:
   idx-=1
 print 't_count', T_count
if __name__ == '__main__':
 with file('./f.txt', 'wb') as o:
  get_flower(21, o)
  #get_flower(3, o)
Copy after login

希望本文所述对大家的Python程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template