本来这题是拿来试试冒泡排序的,结果写了一晚上还没弄好,目前还是不能处理相同的值,以及输出还有问题。
import copy
def sequence_water(lst):
for i in range(len(lst)-1,1,-1):
squence_move_max(lst,i)
def squence_move_max(lst,max_index):
for i in range(max_index):
if lst[i] >= lst[i+1]:
lst[i],lst[i+1] = lst[i+1],lst[i]
if __name__ == "__main__":
person_num = int(input())
lst = input().split(" ")
lst = list(map(int,lst))
old_index = copy.deepcopy(lst)
if len(lst) == person_num:
sequence_water(lst)
for i in range(len(lst)):
location = old_index.index(lst[i])+1
print(location,end='')
print(" ",end='')
print("\n")
wait_time = 0
for index in range(len(lst)-1):
wait_time += lst[index]*(len(lst)-1-index)
print(wait_time/len(lst))
pass
还是有问题,明天改改
修改版的来啦
具体思路是将字典的key和value分别存储,找到位置后替换value的值为一个不会出现的数,比如-1,然后就会解决相同值的问题,如下
def sequence_water(lst):
for i in range(len(lst)-1,1,-1):
squence_move_max(lst,i)
def squence_move_max(lst,max_index):
for i in range(max_index):
if lst[i] >= lst[i+1]:
lst[i],lst[i+1] = lst[i+1],lst[i]
if __name__ == "__main__":
person_num = int(input())
lst = input().split(" ")
lst = list(map(int,lst))
dict_loc = {}
key_tem = []
value_tem = []
for index in range(len(lst)):
loc_tem = {'{}'.format(index):lst[index]}
dict_loc.update(loc_tem)
for key in dict_loc:
key_tem.append(key)
value_tem.append(dict_loc[key])
if len(lst) == person_num:
sequence_water(lst)
for i in range(len(lst)):
for j in range(len(value_tem)):
if lst[i] == value_tem[j]:
print(int(key_tem[j])+1,end="")
print(" ",end="")
value_tem[j] = -1
print("")
wait_time = 0
for index in range(len(lst)-1):
wait_time += lst[index]*(len(lst)-1-index)
print(wait_time/len(lst))