输入格式
第一行一个正整数 N。
接下来 N 行,每行三个整数,其中第 i 行表示第 i 名同学的语文、数学、英语成绩。最先读入的同学编号为 1。
输出格式
输出一个整数,表示“旗鼓相当的对手”的对数。
说明/提示
数据保证,2≤N≤1000 且每科成绩为不超过 150 的自然数。
遍历
class Solution:
@staticmethod
def oi_input():
"""仅读取原始三科成绩"""
n = int(input())
students = []
for _ in range(n):
ch, ma, en = map(int, input().split())
students.append((ch, ma, en))
return n, students
@staticmethod
def oi_test():
"""仅提供原始测试数据"""
return 3, [
(90, 90, 90),
(85, 95, 90),
(80, 100, 91)
]
@staticmethod
def solution(n, raw_students):
"""包含所有数据处理逻辑"""
# 预处理:计算总分
students = [(s[0], s[1], s[2], sum(s)) for s in raw_students]
count = 0
for i in range(n):
for j in range(i + 1, n):
'''先检查总分 再检查各科'''
if abs(students[i][3] - students[j][3]) > 10:
continue
if all(abs(students[i][k] - students[j][k]) <= 5 for k in range(3)):
count += 1
print(count)
oi_input = Solution.oi_input
oi_test = Solution.oi_test
solution = Solution.solution
if __name__ == '__main__':
n, students = oi_test()
# n, students = oi_input()
solution(n, students)
哈希
class Solution:
@staticmethod
def oi_input():
"""仅读取原始三科成绩"""
n = int(input())
students = []
for _ in range(n):
ch, ma, en = map(int, input().split())
students.append((ch, ma, en))
return n, students
@staticmethod
def oi_test():
"""仅提供原始测试数据"""
return 3, [
(90, 90, 90),
(85, 95, 90),
(80, 100, 91)
]
@staticmethod
def solution(n, raw_students):
"""包含所有数据处理逻辑"""
from collections import defaultdict
# 预处理:计算总分
students = [(s[0], s[1], s[2], sum(s)) for s in raw_students]
# 构建总分哈希表 defaultdict 在没有找到值的情况下会,创建空的列表和键 k-v
total_map = defaultdict(list)
for idx, s in enumerate(students):
'''前索引 后s列表'''
total_map[s[3]].append(idx)
count = 0
for i in range(n):
current_total = students[i][3]
# 生成有效总分范围
for t in range(current_total - 10, current_total + 11):
if t not in total_map:
continue
for j in total_map[t]:
'''j > i 防止重复比较'''
if j > i and all(abs(students[i][k] - students[j][k]) <= 5 for k in range(3)):
count += 1
print(count)
oi_input = Solution.oi_input
oi_test = Solution.oi_test
solution = Solution.solution
if __name__ == '__main__':
n, students = oi_test()
# n, students = oi_input()
solution(n, students)