SRM147 DIV1 350

约瑟夫环问题,直接模拟即可,找出被开除的前femaleNum个人的位置,利用这个位置信息构造字符串即可

 1 class PeopleCircle:
 2     def order(self, numMales, numFemales, k):
 3         a = []
 4         s = ['M'] * (numMales+numFemales)
 5         for i in range(0, numMales+numFemales):
 6             a.append(i)
 7         begin = 0
 8         for i in range(0, numFemales):
 9             next = (begin + k - 1) % len(a)
10             s[a.pop(next)] = 'F'
11             if next == len(a):
12                 begin = 0
13             else:
14                 begin = next
15         
16         result = ''
17         for ch in s:
18             result += ch
19         return result
20     
21 # test
22 o = PeopleCircle()
23 
24 # test: len = 0
25 assert(o.order(0, 0, 555) == '')
26 
27 # test
28 assert(o.order(5, 3, 2) == "MFMFMFMM")
29 print('ok')
View Code

 

posted @ 2013-11-01 06:45  valaxy  阅读(128)  评论(0编辑  收藏  举报