当前位置:网站首页>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;
}
边栏推荐
- Binary search basis
- [轉]: OSGI規範 深入淺出
- Simple modal box
- Es module and commonjs learning notes -- ESM and CJS used in nodejs
- Unity ugui source code graphic
- Basic knowledge points of dictionary
- Dotween usage records ----- appendinterval, appendcallback
- To be continued] [UE4 notes] L4 object editing
- Recherche de mots pour leetcode (solution rétrospective)
- [merge array] 88 merge two ordered arrays
猜你喜欢
随机推荐
[turn to] MySQL operation practice (I): Keywords & functions
stm32Cubemx(8):RTC和RTC唤醒中断
[binary search] 34 Find the first and last positions of elements in a sorted array
[turn]: Apache Felix framework configuration properties
Judge the position of the monster in the role under unity3d
Solon Logging 插件的添加器级别控制和日志器的级别控制
[paper notes] multi goal reinforcement learning: challenging robotics environments and request for research
支持多模多态 GBase 8c数据库持续创新重磅升级
[allocation problem] 455 Distribute cookies
Listview is added and deleted at the index
对象的序列化
Unity get component
Embedded database development programming (V) -- DQL
Under the national teacher qualification certificate in the first half of 2022
FVP和Juno平台的Memory Layout介绍
JVM call not used once in ten years
Out and ref functions of unity
Pointnet++的改进
Download xftp7 and xshell7 (official website)
Fragment addition failed error lookup