2015/8/8-出现次数最多的数
2015-08-08 20:51
351 查看
题目名称:出现次数最多的数
问题描述
给定n个正整数,找出它们中出现次数最多的数。如果这样的数有多个,请输出其中最小的一个。
输入格式
输入的第一行只有一个正整数n(1 ≤ n ≤ 1000),表示数字的个数。
输入的第二行有n个整数s1, s2, …, sn (1 ≤ si ≤ 10000, 1 ≤ i ≤ n)。相邻的数用空格分隔。
输出格式
输出这n个次数中出现次数最多的数。如果这样的数有多个,输出其中最小的一个。
样例输入
6
10 1 10 20 30 20
样例输出
10
问题描述
给定n个正整数,找出它们中出现次数最多的数。如果这样的数有多个,请输出其中最小的一个。
输入格式
输入的第一行只有一个正整数n(1 ≤ n ≤ 1000),表示数字的个数。
输入的第二行有n个整数s1, s2, …, sn (1 ≤ si ≤ 10000, 1 ≤ i ≤ n)。相邻的数用空格分隔。
输出格式
输出这n个次数中出现次数最多的数。如果这样的数有多个,输出其中最小的一个。
样例输入
6
10 1 10 20 30 20
样例输出
10
import java.util.*; public class Main { public static void main(String[] args) { new Main().run(); } public void run() { Scanner fin = new Scanner(System.in); int N = fin.nextInt(); int[] count = new int[10001]; for (int i = 0; i < N; ++i) { </span>++count[fin.nextInt()]; } int maxCount = -1; int result = 0; for (int i = 1; i <= 10000; ++i) { if (count[i] > maxCount) { maxCount = count[i]; result = i; } } System.out.println(result); } }
相关文章推荐
- 剑指offer_面试题12_打印1到最大的n位数(大数问题)
- C#高级编程八十天----异常类
- 【剑指Offer面试题】 九度OJ1373:整数中1出现的次数(从1到n整数中1出现的次数)
- [leedcode 239] Sliding Window Maximum
- WebRTC入门学习之初识WebRTC
- UI05_Control(UIStepper加减音量/UIBlurEffect毛玻璃/UISlider滑动/UISegmentedControl小模块)
- Android开发 关于Android性能,内存优化
- python正则表达式 re (二)match与search
- nginx 的限制连接模块limit_zone与limit_req_zone
- 大约xib连接错误bug正确
- 配置Log4J(转载)
- 为什么R.string.ID 返回的是一串数字,而不是字符串?
- 3.6html学习笔记之样式选择
- ubuntu 建立eclipse桌面快捷方式
- C++中数字与字符串之间的转换
- opencv实现简单的局部极大值求解
- 如何实时查看linux下的日志
- 8.8
- 《C程序设计语言》学习笔记——练习3-1
- Access restriction: The type 'Application' is not API (restriction on required library rt.jar)