当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
随机推荐
企业级的dns服务器的搭建
Anconda spyder内引用OpenCV出现错误ImportError: numpy.core.multiarray failed to import
el-input 只能输入整数(包括正数、负数、0)或者只能输入整数(包括正数、负数、0)和小数
单 词替换
JS事件循环机制
剑指Offer 31.栈的压入、弹出
Plus版SBOM:流水线物料清单PBOM
【学习笔记】如何打造运维组织架构
arr的扩展方法、数组的遍历及其他方法
asyncawait和promise的区别
计算属性的学习
samba,nfs,iscsi网络文件系统
剑指Offer 32.Ⅱ从上到下打印二叉树
flasgger手写phpwind接口文档
三维目标检测之OpenPCDet环境配置及demo测试
shell脚本的基础知识
【 LeetCode 】 design list
节流阀和本地存储
未来智安创始人兼CEO唐伽佳荣膺36氪X·36Under36 “S级创业者”
Gartner 权威预测未来4年网络安全的8大发展趋势









