本文实例讲述了python求解水仙花数的方法。分享给大家供大家参考。具体如下:
一个N位的十进制正整数,如果它的每个位上的数字的N次方的和等于这个数本身,则称其为花朵数。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
#!/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) |
希望本文所述对大家的Python程序设计有所帮助。