当前位置:网站首页>Haut OJ 1321: mode problem of choice sister
Haut OJ 1321: mode problem of choice sister
2022-07-05 05:17:00 【hunziHang】
Problem description :
Choice The elder sister returned home , hold n Put candies in boxes , The first i What kinds of candy do you have mi individual , She wants to know mi What is the number that appears most in , Can you tell her ? Ensure that the number with the most occurrences occurs more than n/2.
Input :
Enter an integer in the first line n(1 <= n <= 1e6).
Next line n It's an integer mi(1<= mi <= 1e9), It means the first one i The number of candies , Integers are separated by spaces .
( Pay attention to memory limitations , Mode occurs more than n/2)
Output :
Output mi The number that appears most in , Occupy a line .
The sample input :
5 10 10 10 20 30
Sample output :
10
Cause analysis :
data n Too big , The array stores the number of occurrences of each , Then cycle again to find the maximum , Time must have expired .
So you can One by one , Take a variable and save times , If equal, then the number ++, Otherwise times --, If it's reduced to 0 It means that this number is not a mode , Of course, the number of times -- The number of is certainly not the mode , for instance : 3 3 3 2 2 1 The last number is reduced to 0, 3 2 1 Certainly not the mode
Solution :
#include <stdio.h>
int main()
{
int n, t, x, top;
scanf("%d", &n);
top = t = 0;
for(int i = 1;i <= n; i++){
scanf("%d",&x);
if(x == t)
top++;
else if(top == 0) {
t=x;
top=1;
}
else
top--;
}
printf("%d\n", t);
return 0;
}
边栏推荐
- 2022/7/2做题总结
- Recherche de mots pour leetcode (solution rétrospective)
- Simple HelloWorld color change
- Solon Auth 认证框架使用演示(更简单的认证框架)
- cocos2dx_ Lua particle system
- SDEI初探-透过事务看本质
- Programmers' experience of delivering takeout
- 【论文笔记】Multi-Goal Reinforcement Learning: Challenging Robotics Environments and Request for Research
- win下一键生成当日的时间戳文件
- 质量体系建设之路的分分合合
猜你喜欢

UE 虚幻引擎,项目结构
![[to be continued] [UE4 notes] L1 create and configure items](/img/20/54ba719be2e51b7db5b7645b361e26.jpg)
[to be continued] [UE4 notes] L1 create and configure items

The present is a gift from heaven -- a film review of the journey of the soul

Download and use of font icons

Applet live + e-commerce, if you want to be a new retail e-commerce, use it!

Optimization scheme of win10 virtual machine cluster

Ue4/ue5 illusory engine, material part (III), material optimization at different distances

Reverse one-way linked list of interview questions

Shell Sort

Grail layout and double wing layout
随机推荐
Web APIs DOM节点
PMP candidates, please check the precautions for PMP examination in July
Programmers' experience of delivering takeout
Use of snippets in vscode (code template)
小程序直播+电商,想做新零售电商就用它吧!
Unity writes timetables (without UI)
Quick sort summary
Recherche de mots pour leetcode (solution rétrospective)
Cocos2dx Lua registers the touch event and detects whether the click coordinates are within the specified area
[轉]: OSGI規範 深入淺出
[binary search] 69 Square root of X
发现一个很好的 Solon 框架试手的教学视频(Solon,轻量级应用开发框架)
cocos2dx_ Lua card flip
django连接数据库报错,这是什么原因
Cocos create Jiugongge pictures
Common technologies of unity
2022年上半年国家教师资格证考试
Use the command character to close the keyboard command of the notebook
Page countdown
Ue4/ue5 illusory engine, material part (III), material optimization at different distances