解题思路:
注意事项:
参考代码:
一、 按顺序传递 不同时发生(利用列表的前后循环) 超时9
1. 从第一位开始传递一半给最后一位,接着从最后一位开始传递给第二位。依次逆时针传递
2. 传递结束后需要给奇数个数添加1
3. 判断糖果是否满足
# 模拟题目倒序遍历 def EqualSweet(SweetList): if len(set(SweetList)) != 1: return False return True n = int(input().strip()) SweetList = [int(temp) for temp in input().strip().split()] count = 0 while not EqualSweet(SweetList): for i in range(n - 1, -1, -1): temp = SweetList[i - n + 1] // 2 SweetList[i] += temp SweetList[i - n + 1] -= temp print(SweetList) # 使用foreach的话需要把值重新赋值到List中 for i in range(len(SweetList)): if SweetList[i] % 2 != 0: SweetList[i] += 1 count += 1 print(SweetList) print(count)
二、 同时传递 AC(每位小朋友保持自己的一半 同时获得前一位小朋友手中的一半)
1. 创建错序列表 错位相加
2. 传递结束后需要给奇数个数添加1
3. 判断糖果是否满足
# 模拟题目倒序遍历 from copy import deepcopy def EqualSweet(SweetList): if len(set(SweetList)) != 1: return False return True n = int(input().strip()) SweetList = [int(temp) for temp in input().strip().split()] count = 0 while not EqualSweet(SweetList): # 错位处理 temp = deepcopy(SweetList) # 把末尾放置在首位 相当于从首位开始取给最后一位 first = temp.pop() temp.insert(0, first) print(temp) # 相当于从第二位开始取开始 即第n个小朋友手中保持自己的一半与前一个小朋友给的一半 # first = temp.pop(0) # temp.append(first) SweetList = [SweetList[i] // 2 + temp[i] // 2 for i in range(n)] print(SweetList) for i in range(len(SweetList)): if SweetList[i] % 2 != 0: SweetList[i] += 1 count += 1 print(count)
0.0分
3 人评分