当前位置:网站首页>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;
}
边栏推荐
- C language Essay 1
- Generate filled text and pictures
- 远程升级怕截胡?详解FOTA安全升级
- [LeetCode] 整数反转【7】
- To the distance we have been looking for -- film review of "flying house journey"
- Do a small pressure test with JMeter tool
- [speed pointer] 142 circular linked list II
- Common technologies of unity
- [转]: OSGI规范 深入浅出
- Insert sort
猜你喜欢
随机推荐
Page countdown
Lua determines whether the current time is the time of the day
PMP candidates, please check the precautions for PMP examination in July
[leetcode] integer inversion [7]
[to be continued] [depth first search] 547 Number of provinces
《动手学深度学习》学习笔记
Quick sort summary
Data is stored in the form of table
Time format conversion
Unity shot tracking object
PMP考试敏捷占比有多少?解疑
Django reports an error when connecting to the database. What is the reason
Unity intelligent NPC production -- pre judgment walking (method 1)
Out and ref functions of unity
[LeetCode] 整数反转【7】
Personal required code
小程序直播+電商,想做新零售電商就用它吧!
Collapse of adjacent vertical outer margins
对象的序列化
[转]MySQL操作实战(三):表联结

![[轉]: OSGI規範 深入淺出](/img/54/d73a8d3e375dfe430c2eca39617b9c.png)







![[turn to] MySQL operation practice (III): table connection](/img/70/20bf9b379ce58761bae9955982a158.png)