| 
 | 
 
 
发表于 2018-3-23 17:47:27
|
显示全部楼层
 
 
 
num  = input('请输入掷骰子的次数:') 
num = int(num) 
list1 = [] 
import random 
n=0 
while n<num: 
    i = random.randint(1,6) 
    j = random.randint(1,6) 
    k = random.randint(1,6) 
    tup = (i,j,k) 
    list1.append(tup) 
    n +=1 
print('原掷骰子数据:',list1) 
 
print('--------------------------------------------------------') 
 
#三个一样 
listsame = [] 
#两个一样 
listtwosame = [] 
#都不一样的 
listnosame = [] 
 
for each in list1: 
    if max(each) == min(each): 
        listsame.append(each) 
    elif( each[0] != each[1] and each[0]!=each[2] and each[1]!= each[2]): 
        listnosame.append(each) 
    else: 
        listtwosame.append(each) 
 
listresult = [] 
 
#分别对listsame listtwosame listnosame 进行排序 
listsame = sorted(listsame,reverse=True) 
#对listnosame 进行排序 
for each in listnosame: 
    for i in range(len(listnosame)-1): 
        for j in range(len(listnosame)-i-1): 
            if  listnosame[j][0]+listnosame[j][1]+listnosame[j][2]< listnosame[j+1][0]+listnosame[j+1][1]+listnosame[j+1][2] : 
                listnosame[j], listnosame[j+1] = listnosame[j+1], listnosame[j] 
 
#对listtwosame 进行排序 
def compare(tup): 
    same =0 
    nosame =0 
    if(tup[0]==tup[1]): 
        same = tup[0] 
        nosame = tup[2] 
    elif(tup[0]==tup[2]): 
        same = tup[0] 
        nosame = tup[1] 
    else: 
        same = tup[1] 
        nosame = tup[0] 
    tup2 = (same,nosame) 
    return tup2 
 
for each in listtwosame: 
    for i in range(len(listtwosame)-1):    # 这个循环负责设置冒泡排序进行的次数 
        for j in range(len(listtwosame)-i-1):  # j为列表下标 
            if compare(listtwosame[j])[0] < compare(listtwosame[j+1])[0]: 
                listtwosame[j], listtwosame[j+1] = listtwosame[j+1], listtwosame[j] 
            elif compare(listtwosame[j])[0] == compare(listtwosame[j+1])[0]: 
                if compare(listtwosame[j])[1] < compare(listtwosame[j+1])[1]: 
                    listtwosame[j], listtwosame[j + 1] = listtwosame[j + 1], listtwosame[j] 
if  listsame: 
    listresult.extend(listsame) 
if  listtwosame: 
    listresult.extend(listtwosame) 
if  listnosame: 
    listresult.extend(listnosame) 
print('排序后数据:',listresult) 
 
 
请输入掷骰子的次数:12 
原掷骰子数据: [(6, 2, 5), (2, 5, 6), (2, 2, 5), (3, 2, 4), (3, 6, 3), (5, 5, 4), (6, 3, 1), (2, 6, 2), (2, 1, 1), (1, 3, 3), (6, 1, 6), (3, 4, 2)] 
-------------------------------------------------------- 
排序后数据: [(6, 1, 6), (5, 5, 4), (3, 6, 3), (1, 3, 3), (2, 6, 2), (2, 2, 5), (2, 1, 1), (6, 2, 5), (2, 5, 6), (6, 3, 1), (3, 2, 4), (3, 4, 2)] |   
 
评分
- 
查看全部评分
 
 
 
 
 
 |