当前位置:网站首页>CCF刷题之旅--第一题
CCF刷题之旅--第一题
2022-08-02 03:33:00 【风华同学】
寻找出现次数最多的数
题目描述
给定n个正整数,找出它们中出现次数最多的数。如果这样的数有多个,请输出其中最小的一个。
输入
输入的第一行只有一个正整数n(1 ≤ n ≤ 1000),表示数字的个数。输入的第二行有n个整数s1, s2, …, sn (1 ≤ si ≤ 10000, 1 ≤ i ≤ n)。相邻的数用空格分隔。
输出
输出这n个次数中出现次数最多的数。如果这样的数有多个,输出其中最小的一个。
#include <iostream>
using namespace std;
/* * * 利用哈希表来表示每个数出现的次数 * step1:创建一个哈希数组 * step2:每个数字带表了数组的下标 * step3:,每次出现一个数字则数组的值加一 * step4:在通过遍历数组找到数组的值最大的那个并且将其赋值给要求的出现次数最多的数字(由于数组的下标是从0开始的,所以不需要另外比较相同次数中较小的那个数字) */
int main()
{
int s[1000] = {
0 };
int n;
int num,ans;
int flag = 0;//标志着每个数字出现的次数
int max = 0;//找到最大的数字
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)//这里为>而不是>=(用来比较出现次数的多少)
{
flag = s[i];
ans = i;//ans代表着找到的那个数字
}
}
cout << ans;
}
边栏推荐
- 分割回文串 DP+回溯 (LeetCode-131)
- Comparative analysis of OneNET Studio and IoT Studio
- 2019 - ICCV - 图像修复 Image Inpainting 论文导读《StructureFlow: Image Inpainting via Structure-aware ~~》
- 使用pyqt弹出消息提示框
- Type c PD 电路设计
- 【Popular Science Post】Detailed explanation of MDIO interface
- Modify hosts file using batch script
- Application of electronic flow on business trip
- 【plang1.4.3】语言新特性:集合
- 【LeetCode】设计链表
猜你喜欢
【网络基础】浏览器输入一个URL之后,都发生了什么(详细讲解)
PCIE电路设计
【操作系统】线程安全保护机制
联阳IT6561|IT6561FN方案电路|替代IT6561方案设计DP转HDMI音视频转换器资料
[Arduino connected to GP2Y1014AU0F dust sensor]
【TCS3200 color sensor and Arduino realize color recognition】
Arduino lights up nixie tubes
与TI的lvds芯片兼容-GM8284DD,GM8285C,GM8913,GM8914,GM8905C,GM8906C,国腾振芯LVDS类芯片,
[DS3231 RTC real-time clock module and Arduino interface to build a digital clock]
Personal image bed construction based on Alibaba Cloud OSS+PicGo
随机推荐
振芯科技GM8285C:功能TTL转LVDS芯片简介
蛮力法求解凸包问题
[Arduino uses a rotary encoder module]
WebApp 在线编程成趋势:如何在 iPad、Matepad 上编程?
联阳IT6561|IT6561FN方案电路|替代IT6561方案设计DP转HDMI音视频转换器资料
PCIE电路设计
Case | industrial iot solutions, steel mills high-performance security for wisdom
OneNET Studio与IoT Studio对比分析
【plang 1.4.4】编写茶几玛丽脚本
【nRF24L01 connects with Arduino to realize wireless communication】
简单的RC滤波电路
进程(番外):自定义shell命令行解释器
【TCS3200 color sensor and Arduino realize color recognition】
Comparative analysis of mobile cloud IoT pre-research and Alibaba Cloud development
NSIS来自己设定快捷方式的图标
MAC安装Mysql超详细完整教程
增量编译技术在Lightly中的实践
I2C无法访问ATEC508A加密芯片问题
NE5532运放加法器
回溯法 & 分支限界 - 2