1. 程式人生 > 其它 >給定一個射擊比賽成績單,包含多個選手若干次射擊的成績分數,請對每個選手按其最高三個分數之和進行降序排名,輸出降序排名後的選手id序列

給定一個射擊比賽成績單,包含多個選手若干次射擊的成績分數,請對每個選手按其最高三個分數之和進行降序排名,輸出降序排名後的選手id序列

給定一個射擊比賽成績單,包含多個選手若干次射擊的成績分數,請對每個選手按其最高三個分數之和進行降序排名,輸出降序排名後的選手id序列
條件如下:
1. 一個選手可以有多個射擊成績的分數,且次序不固定
2. 如果一個選手成績少於3個,則認為選手的所有成績無效,排名忽略該選手
3. 如果選手的成績之和相等,則相等的選手按照其id降序排列

輸入描述:
輸入第一行
一個整數N
表示該場比賽總共進行了N次射擊
產生N個成績分數
2<=N<=100
輸入第二行
一個長度為N整數序列
表示參與每次射擊的選手id
0<=id<=99
輸入第三行
一個長度為N整數序列
表示參與每次射擊選手對應的成績
0<=成績<=100

輸出描述:
符合題設條件的降序排名後的選手ID序列

示例一
輸入:
13
3,3,7,4,4,4,4,7,7,3,5,5,5
53,80,68,24,39,76,66,16,100,55,53,80,55
輸出:
5,3,7,4
說明:
該場射擊比賽進行了13次
參賽的選手為{3,4,5,7}
3號選手成績53,80,55 最高三個成績的和為188
4號選手成績24,39,76,66 最高三個成績的和為181
5號選手成績53,80,55 最高三個成績的和為188
7號選手成績68,16,100 最高三個成績的和為184
比較各個選手最高3個成績的和
有3號=5號>7號>4號
由於3號和5號成績相等 且id 5>3
所以輸出5,3,7,4

"""
方法一:
"""
import
java.util.*; import java.util.stream.Collectors; public class t1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = Integer.parseInt(in.nextLine()); List<Integer> ids = toIntList(in.nextLine()); List<Integer> scores = toIntList(in
.nextLine()); in.close(); HashMap<Integer, List<Integer>> map = new HashMap<>(); for (int i = 0; i < n; i++) { Integer id = ids.get(i); Integer score = scores.get(i); List<Integer> list = map.getOrDefault(id, new LinkedList<>()); list.add(score); map.put(id, list); } StringBuilder builder = new StringBuilder(); map.entrySet() .stream() .filter(x -> x.getValue().size() >= 3) .sorted((o1, o2) -> { Integer sum1 = sumT3(o1.getValue()); Integer sum2 = sumT3(o2.getValue()); if (sum1.equals(sum2)) { return o2.getKey() - o1.getKey(); } else { return sum2 - sum1; } }) .map(Map.Entry::getKey) .forEach(x -> builder.append(x).append(",")); System.out.println(builder.substring(0, builder.length() - 1)); } private static Integer sumT3(List<Integer> list) { list.sort(Integer::compareTo); int sum = 0; for (int i = list.size() - 1; i >= list.size() - 3; i--) { sum += list.get(i); } return sum; } private static List<Integer> toIntList(String str) { return Arrays.stream(str.split(",")) .map(Integer::parseInt) .collect(Collectors.toList()); } }
"""
方法二:
"""
def
get_result(num, ids, scores): data = {} for i in range(num): if ids[i] not in data: data[ids[i]] = [] data[ids[i]].append(scores[i]) _data = [] for i, j in data.items(): if len(j) < 3: continue _data.append([i, sum(sorted(j, reverse=True)[:3])]) _data.sort(key=lambda x: -x[1]) return ",".join(str(i[0]) for i in _data) if __name__ == '__main__': num = int(input()) ids = [int(id) for id in input().strip().split(',')] scores = [int(score) for score in input().strip().split(',')] print(get_result(num, ids, scores))