当前位置:网站首页>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;
}
边栏推荐
- 小程序直播+电商,想做新零售电商就用它吧!
- Merge sort
- 2022上半年全国教师资格证下
- Ue4/ue5 illusory engine, material part (III), material optimization at different distances
- Generate filled text and pictures
- Grail layout and double wing layout
- Listview is added and deleted at the index
- TF-A中的工具介绍
- [转]: OSGI规范 深入浅出
- Research on the value of background repeat of background tiling
猜你喜欢
小程序直播+电商,想做新零售电商就用它吧!
Applet Live + e - commerce, si vous voulez être un nouveau e - commerce de détail, utilisez - le!
2022/7/2 question summary
TF-A中的工具介绍
[to be continued] [UE4 notes] L2 interface introduction
To the distance we have been looking for -- film review of "flying house journey"
[转]MySQL操作实战(三):表联结
Download and use of font icons
The present is a gift from heaven -- a film review of the journey of the soul
Grail layout and double wing layout
随机推荐
TF-A中的工具介绍
[转]: OSGI规范 深入浅出
使用命令符关闭笔记本自带键盘命令
National teacher qualification examination in the first half of 2022
Unity shot tracking object
xftp7与xshell7下载(官网)
发现一个很好的 Solon 框架试手的教学视频(Solon,轻量级应用开发框架)
[leetcode] integer inversion [7]
小程序直播+電商,想做新零售電商就用它吧!
To be continued] [UE4 notes] L4 object editing
GBase数据库助力湾区数字金融发展
To the distance we have been looking for -- film review of "flying house journey"
Judge the position of the monster in the role under unity3d
嵌入式数据库开发编程(六)——C API
[turn]: Apache Felix framework configuration properties
Kali 2018 full image download
Bucket sort
When will Wei Lai, who has been watched by public opinion, start to "build high-rise buildings" again?
UE4/UE5 虚幻引擎,材质篇,纹理,Compression and Memory压缩和内存
The next key of win generates the timestamp file of the current day