|
发表于 2017-2-20 13:41:54
|
显示全部楼层
for用的太多,自己都有点绕了
- def fen(jh): #把xxxx分成[xx,xx]的形式,4位数分成两个两位数,排除后面的两位数小于10的情况
- ls=[]
- for i in jh:
- if i%100>9:
- ls.append([i//100,i%100])
- return ls
- jhsan,jhsi,jhwu,jhliu,jhqi,jhba=[],[],[],[],[],[]
- for x in range(15,150):#根据题目的公式,把6种形式的4位数分别放入对应集合
- if x*(x+1)/2>1000 and x*(x+1)/2<9999 :jhsan.append(int(x*(x+1)/2))
- if x**2>1000 and x**2<9999 :jhsi.append(x**2)
- if x*(3*x-1)/2>1000 and x*(3*x-1)/2<9999 :jhwu.append(int(x*(3*x-1)/2))
- if x*(2*x-1)>1000 and x*(2*x-1)<9999 :jhliu.append(x*(2*x-1))
- if x*(5*x-3)/2>1000 and x*(5*x-3)/2<9999 :jhqi.append(int(x*(5*x-3)/2))
- if x*(3*x-2)>1000 and x*(3*x-2)<9999 :jhba.append(x*(3*x-2))
- jhsan,jhsi,jhwu,jhliu,jhqi,jhba=fen(jhsan),fen(jhsi),fen(jhwu),fen(jhliu),fen(jhqi),fen(jhba) #6个集合的数分裂一下
- cs=[[],[],[],[],[],[]]#数字分别带入之后测试是否满足条件的列表
- huizong0=[jhsan,jhsi,jhwu,jhliu,jhqi,jhba]#汇总6个集合的数,huizong0-5决定第一个数到第六个数的取值范围
- for s0 in huizong0: #huizong0里面是第一个数的可选范围
- huizong1=huizong0.copy()
- huizong1.remove(s0)
- for s1 in huizong1: #huizong1里面是把huizong0里面剔除一个集合之后的汇总,也就是第二个数的备选范围
- huizong2=huizong1.copy()
- huizong2.remove(s1)
- for s2 in huizong2: #下面直到huizong5都是以此类推
- huizong3=huizong2.copy()
- huizong3.remove(s2)
- for s3 in huizong3:
- huizong4=huizong3.copy()
- huizong4.remove(s3)
- for s4 in huizong4:
- huizong5=huizong4.copy()
- huizong5.remove(s4)
- for s5 in huizong5:
- for cs[0] in s0: #从这里开始是真正的验证排序是否符合题目要求,符合要求之后每两个两位数合成一个4位数
- for cs[1] in s1:
- if cs[0][1]!=cs[1][0]:continue
- for cs[2] in s2:
- if cs[1][1]!=cs[2][0]:continue
- for cs[3] in s3:
- if cs[2][1]!=cs[3][0]:continue
- for cs[4] in s4:
- if cs[3][1]!=cs[4][0]:continue
- for cs[5] in s5:
- if cs[4][1]!=cs[5][0]:continue
- if cs[5][1]==cs[0][0]:
- cs=[cs[0][0]*100+cs[0][1],cs[1][0]*100+cs[1][1],cs[2][0]*100+cs[2][1],cs[3][0]*100+cs[3][1],cs[4][0]*100+cs[4][1],cs[5][0]*100+cs[5][1]]
- print(cs,sum(cs))
- exit()
复制代码
- == RESTART: C:\Users\ASUS\AppData\Local\Programs\Python\Python35-32\test.py ==
- [8256, 5625, 2512, 1281, 8128, 2882] 28684
- >>>
复制代码
|
评分
-
查看全部评分
|