当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
随机推荐
jni中jstring与char*互转
Scalar value for argument ‘color‘ is not numeric错误处理
剩余参数、数组对象的方法和字符串扩展的方法
腾讯云+keepalived搭建云服务器主备实践
剑指Offer 16.数值的整数次方 快速幂+ 递归
单 词替换
Class ‘PHPWord_Writer_Word2003‘ not found
5个开源组件管理小技巧
VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tupl
允许Traceroute探测
自研用户登录鉴权机制流程与实现过程
el-dropdown(下拉菜单)的入门学习
MySQL读写分离mysql-proxy部署
Transfer of UKlog.dat and QQ, WeChat files
windows系统下php-ffmpeg类库的使用
侦听器watch及其和计算属性、methods方法的总结
STM32/TMS320F2812+W5500硬软件调试总结
位居榜首 | 未来智安荣登CCIA「2022年中国网安产业潜力之星」榜单
倒排单词
深度学习基础之过拟合、欠拟合问题和正则化