当前位置:网站首页>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;
}
边栏推荐
- Unity card flipping effect
- Binary search basis
- C语言杂谈1
- Reverse one-way linked list of interview questions
- Transport connection management of TCP
- 2022年上半年国家教师资格证考试
- Dotween usage records ----- appendinterval, appendcallback
- Romance of programmers on Valentine's Day
- Leetcode word search (backtracking method)
- stm32Cubemx(8):RTC和RTC唤醒中断
猜你喜欢

Binary search basis
![[interval problem] 435 Non overlapping interval](/img/a3/2911ee72635b93b6430c2efd05ec9a.jpg)
[interval problem] 435 Non overlapping interval

十年不用一次的JVM调用

Shell Sort

Quick sort summary

Unity ugui source code graphic

Learning notes of "hands on learning in depth"

stm32Cubemx(8):RTC和RTC唤醒中断

Django reports an error when connecting to the database. What is the reason

National teacher qualification examination in the first half of 2022
随机推荐
使用Room数据库报警告: Schema export directory is not provided to the annotation processor so we cannot expor
小程序直播+電商,想做新零售電商就用它吧!
Unity parallax infinite scrolling background
用 Jmeter 工具做个小型压力测试
64 horses, 8 tracks, how many times does it take to find the fastest 4 horses at least
Embedded database development programming (V) -- DQL
Quick sort summary
服务熔断 Hystrix
被舆论盯上的蔚来,何时再次“起高楼”?
win10虚拟机集群优化方案
发现一个很好的 Solon 框架试手的教学视频(Solon,轻量级应用开发框架)
A complete attack chain
Double pointer Foundation
Unity shot tracking object
2022/7/2 question summary
Unity ugui source code graphic
Ue4/ue5 illusory engine, material part (III), material optimization at different distances
Database under unity
[to be continued] [UE4 notes] L3 import resources and project migration
Panel panel of UI