题目:n个人玩牌,需要每个人手中的牌数之和相等并为最大值
思路:每人手中的最大牌数之和为target-sum(总牌数和/n*2)
小函数内循环找到一个数下标构成target-sum,并添加至pair的配对list里
注意:为了避免重复使用,所以已配对的数不可再用(用False和True作为标记)
def get_pair(cards, start_index, target_sum,is_used):
pair = [start_index]
for i in range(start_index + 1, len(cards)):
if (cards[i] + cards[start_index] == target_sum) and (is_used[i] == False):
pair.append(i)
return pair
n = int(raw_input())
cards = map(int, raw_input().split())
target_sum = (sum(cards) * 2)/n
is_used = [False] * n
for i in range(n):
if is_used[i] == False:
pair = get_pair(cards, i, target_sum,is_used)
is_used[pair[0]] = True
is_used[pair[1]] = True
print "{0} {1}".format(pair[0] + 1, pair[1] + 1)