列表

详情


NC25070. [USACO 2007 Ope l]Popularity

描述

Like many, Bessie is concerned with popularity. In some things, she wants to be just like all the other cows.
She has tabulated a sorted list of the number of ounces of milk (1 <= ounces <= 10,000) that each of the N (2 <= N <= 20,000) cows gives each day. She wants to know which number appears most often in the list and is just positive that the number is unique.

输入描述

Line 1: A single integer: N
Lines 2..N+1: Line i contains a single integer that is the ounces of milk given by cow i.

输出描述

Line 1: The number that appears most often in the input list

示例1

输入:

5
512
532
532
585
599

输出:

532

说明:

Five cows, each giving 512-599 ounces of milk.
532 appears twice; the other numbers appear but once

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Python3 解法, 执行用时: 71ms, 内存消耗: 5216K, 提交时间: 2023-08-15 09:05:57

from collections import defaultdict
ans = 0
m = defaultdict(int)
for i in range(int(input())):
    c = int(input())
    m[c] += 1
    if m[c] > m[ans]:
        ans = c
print(ans)

Python3 解法, 执行用时: 56ms, 内存消耗: 5212K, 提交时间: 2023-08-15 07:52:17

from collections import defaultdict
ans = 0
num = 0
m = defaultdict(int)
for i in range(int(input())):
    c = int(input())
    m[c] += 1
    if m[c] > num:
        num = m[c]
        ans = c
print(ans)

上一题