蓝桥杯--数字排列的Python解法
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                蓝桥杯--数字排列的Python解法
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.                        
                                
                            
                            
                            今有7對數(shù)字:兩個(gè)1,兩個(gè)2,兩個(gè)3,...兩個(gè)7,把它們排成一行。
要求,兩個(gè)1間有1個(gè)其它數(shù)字,兩個(gè)2間有2個(gè)其它數(shù)字,以此類推,
兩個(gè)7之間有7個(gè)其它數(shù)字。如下就是一個(gè)符合要求的排列:
17126425374635
 
當(dāng)然,如果把它倒過來,也是符合要求的。
請你找出另一種符合要求的排列法,并且這個(gè)排列法是以74開頭的。
  
這里用Python解決可以利用生成器的強(qiáng)大回溯能力,可以參考一下8皇后的Python解法。 在各種編程語言實(shí)現(xiàn)的算法中Python的這個(gè)還是相當(dāng)精練巧妙的,當(dāng)然了性能就不好說了
#!/usr/bin/env python # -*- coding: utf-8 -*-def solution(num_list, slots):n = num_list[0]for pos in range(len(slots)-n-1):if slots[pos] == 0 and slots[pos+n+1] == 0:temp = list(slots)temp[pos] = ntemp[pos+n+1] = nif n == 1:yield tempelse:for result in solution(num_list[1:], temp):yield resultinit = (7, 4, 0, 0, 0, 0, 4, 0, 7, 0, 0, 0, 0, 0) nums = (6,5,3,2,1)for s in solution(num_list=nums, slots=init):print(''.join(map(str, s)))# 74151643752362
 
如果要找出所有符合模式的排列還要省事一些:
#!/usr/bin/env python # -*- coding: utf-8 -*-def solution(n, slots):for pos in range(len(slots)-n-1):if slots[pos] == 0 and slots[pos+n+1] == 0:temp = list(slots)temp[pos] = ntemp[pos+n+1] = nif n == 1:yield tempelse:for result in solution(n-1, temp):yield resultfor s in solution(n=7, slots=[0]*14):print(''.join(map(str, s)))
輸出結(jié)果: 73625324765141
72632453764151
72462354736151
73161345726425
71416354732652
71316435724625
74151643752362
72452634753161
57263254376141
37463254276151
57416154372632
57236253471614
17126425374635
57141653472362
17125623475364
27423564371516
62742356437151
26721514637543
36713145627425
51716254237643
23726351417654
41716425327635
52732653417164
35743625427161
35723625417164
24723645317165
56171354632742
46171452632753
16172452634753
46171435623725
53672352461714
45671415362732
34673245261715
52472654131763
34573641512762
15173465324726
61517346532472
46357432652171
26327435614175
53647352462171
41617435263275
23627345161475
15167245236473
14167345236275
16135743625427
26325734615147
52642753461317
25623745361417
52462754316137
15163745326427
15146735423627
14156742352637
>>>?
 
 
                            
                        
                        
                        要求,兩個(gè)1間有1個(gè)其它數(shù)字,兩個(gè)2間有2個(gè)其它數(shù)字,以此類推,
兩個(gè)7之間有7個(gè)其它數(shù)字。如下就是一個(gè)符合要求的排列:
17126425374635
當(dāng)然,如果把它倒過來,也是符合要求的。
請你找出另一種符合要求的排列法,并且這個(gè)排列法是以74開頭的。
注意:只填寫這個(gè)14位的整數(shù),不能填寫任何多余的內(nèi)容,比如說明注釋等。
這里用Python解決可以利用生成器的強(qiáng)大回溯能力,可以參考一下8皇后的Python解法。 在各種編程語言實(shí)現(xiàn)的算法中Python的這個(gè)還是相當(dāng)精練巧妙的,當(dāng)然了性能就不好說了
#!/usr/bin/env python # -*- coding: utf-8 -*-def solution(num_list, slots):n = num_list[0]for pos in range(len(slots)-n-1):if slots[pos] == 0 and slots[pos+n+1] == 0:temp = list(slots)temp[pos] = ntemp[pos+n+1] = nif n == 1:yield tempelse:for result in solution(num_list[1:], temp):yield resultinit = (7, 4, 0, 0, 0, 0, 4, 0, 7, 0, 0, 0, 0, 0) nums = (6,5,3,2,1)for s in solution(num_list=nums, slots=init):print(''.join(map(str, s)))# 74151643752362
如果要找出所有符合模式的排列還要省事一些:
#!/usr/bin/env python # -*- coding: utf-8 -*-def solution(n, slots):for pos in range(len(slots)-n-1):if slots[pos] == 0 and slots[pos+n+1] == 0:temp = list(slots)temp[pos] = ntemp[pos+n+1] = nif n == 1:yield tempelse:for result in solution(n-1, temp):yield resultfor s in solution(n=7, slots=[0]*14):print(''.join(map(str, s)))
輸出結(jié)果: 73625324765141
72632453764151
72462354736151
73161345726425
71416354732652
71316435724625
74151643752362
72452634753161
57263254376141
37463254276151
57416154372632
57236253471614
17126425374635
57141653472362
17125623475364
27423564371516
62742356437151
26721514637543
36713145627425
51716254237643
23726351417654
41716425327635
52732653417164
35743625427161
35723625417164
24723645317165
56171354632742
46171452632753
16172452634753
46171435623725
53672352461714
45671415362732
34673245261715
52472654131763
34573641512762
15173465324726
61517346532472
46357432652171
26327435614175
53647352462171
41617435263275
23627345161475
15167245236473
14167345236275
16135743625427
26325734615147
52642753461317
25623745361417
52462754316137
15163745326427
15146735423627
14156742352637
>>>?
總結(jié)
以上是生活随笔為你收集整理的蓝桥杯--数字排列的Python解法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 选择可解释性高的机器学习模型,而不是决策
- 下一篇: 【每天一个java设计模式(十七)】 -
