当前位置:网站首页>The CCF brush topic tour - the first topic
The CCF brush topic tour - the first topic
2022-08-02 05:17:00 【Fenghua classmate】
Find the number with the most occurrences
题目描述
给定n个正整数,找出它们中出现次数最多的数.如果这样的数有多个,请输出其中最小的一个.
输入
输入的第一行只有一个正整数n(1 ≤ n ≤ 1000),表示数字的个数.输入的第二行有n个整数s1, s2, …, sn (1 ≤ si ≤ 10000, 1 ≤ i ≤ n).相邻的数用空格分隔.
输出
输出这n个次数中出现次数最多的数.如果这样的数有多个,输出其中最小的一个.
#include <iostream>
using namespace std;
/* * * Use a hash table to represent the number of occurrences of each number * step1:创建一个哈希数组 * step2:Each number band indicates the subscript of the array * step3:,Each time a number occurs, the value of the array is incremented by one * step4:Find the one with the largest value in the array by traversing the array and assign it to the requested number with the most occurrences(由于数组的下标是从0开始的,So there is no need to additionally compare the smaller number of the same number of times) */
int main()
{
int s[1000] = {
0 };
int n;
int num,ans;
int flag = 0;//Indicates the number of occurrences of each digit
int max = 0;//find the largest number
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> num;
s[num]++;
if (num > max)
{
max = num;
}
}
for (int i = 0; i <= max; i++)
{
if (s[i] > flag)//这里为>而不是>=(Used to compare the number of occurrences)
{
flag = s[i];
ans = i;//ansrepresents the number found
}
}
cout << ans;
}
边栏推荐
猜你喜欢
随机推荐
全排列 DFS
可视水印的实现——1使用加法实现(add,+)
OpenCV内阈值处理方法
ffmpeg视频播放、格式转化、缩放等命令
微服务入门
盒子移动和滚动加载效果练习
Gartner 权威预测未来4年网络安全的8大发展趋势
PHP实现阿里云HMAC-SHA1签名方法封装
Win8.1下QT4.8集成开发环境的搭建
设置图片纵横比
二进制中1的个数
rosdep update failure solution (pro-test effective)
Class ‘PHPWord_Writer_Word2003‘ not found
VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tupl
剑指Offer 16.数值的整数次方 快速幂+ 递归
剑指Offer 04.二位数组中的查找 线性查找
其他语法和模块的导出导入
逆序对数量与归并排序
未来智安XDR及核心组件产品上榜《嘶吼2022网络安全产业图谱》
允许Traceroute探测