比这篇新的文章: css
比这篇旧的文章: Windows窗口消息大全

cycle decomposition

语言: Python, 标签: permutation python 2008/07/08发布 6个月前更新 更新记录
作者: innersmile, 点击563次, 评论(5), 收藏者(0)

开关行号, 全选(Ctrl+C复制) | 一键复制:HTML, BBCode(Discuz!) , 源代码 | 查看:裸代码, 全屏
背景
主题: 字体:
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
打分:

所有评论,共5条:( 我也来说两句)

1
半瓶墨水 6个月前 回复
0
1
...o|rz...评论被删除...
2
innersmile 6个月前 回复
0
0
@1:

3
innersmile 6个月前 回复
0
0
怎么删除?
4
半瓶墨水 6个月前 回复
0
0
目前没有提供删除功能  
5
innersmile 6个月前 回复
0
0
加油   

发表评论

注册登录后再发表评论