比这篇新的文章:
css
比这篇旧的文章: Windows窗口消息大全
作者: innersmile, 点击563次, 评论(5), 收藏者(0)
打分:
所有评论,共5条:( 我也来说两句)
比这篇旧的文章: Windows窗口消息大全
cycle decomposition
语言: Python, 标签: permutation python 2008/07/08发布 6个月前更新 更新记录作者: innersmile, 点击563次, 评论(5), 收藏者(0)
Python语言: cycle decomposition
01 def gcd(a,b):
02 """Return greatest common divisor using Euclid's Algorithm."""
03 while b:
04 a, b = b, a % b
05 return a
06
07 def lcm(a,b):
08 return a*(b/gcd(a,b))
09
10 def cycle_decomposition(p):
11 n = len(p)
12 cycles = 1
13 for i in range(n):
14 if p[i]>=0: # if this item has not been visited
15 length = 0 # length of current cycle starting with i
16 j = i # j is the moving index in the cycle starting with i
17 while j!=i or length==0:
18 tmp = j
19 length += 1
20 j = p[j]
21 p[tmp] = -1 # indicate that this item has been visited
22 #print length
23 cycles = lcm(cycles,length) #
24 print cycles
25 return cycles
02 """Return greatest common divisor using Euclid's Algorithm."""
03 while b:
04 a, b = b, a % b
05 return a
06
07 def lcm(a,b):
08 return a*(b/gcd(a,b))
09
10 def cycle_decomposition(p):
11 n = len(p)
12 cycles = 1
13 for i in range(n):
14 if p[i]>=0: # if this item has not been visited
15 length = 0 # length of current cycle starting with i
16 j = i # j is the moving index in the cycle starting with i
17 while j!=i or length==0:
18 tmp = j
19 length += 1
20 j = p[j]
21 p[tmp] = -1 # indicate that this item has been visited
22 #print length
23 cycles = lcm(cycles,length) #
24 print cycles
25 return cycles
所有评论,共5条:( 我也来说两句)
| 1 |
半瓶墨水
6个月前
回复
|
| 2 |
innersmile
6个月前
回复
@1:
|
| 3 |
innersmile
6个月前
回复
怎么删除?
|
| 4 |
目前没有提供删除功能
|
| 5 |
innersmile
6个月前
回复
加油
|
代码