stl简单题

本文提供了一种使用 C++ 和 map 数据结构解决 HDU 1029 问题的方法。该问题旨在寻找一个整数数组中出现次数超过一半的元素。文章通过清晰的代码示例展示了如何初始化 map、遍历输入并统计每个元素的出现频率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

hdoj 1029 kuangbindp专题 一时间没想好怎么用dp 先用map水一哈 想好再修改

之前做这个题的时候一直wa 很气

发现map数组没有清空 mdzz

#include <iostream> 

#include <algorithm> 
#include <cstdio> 
#include <cstring>
#include<cmath>
#include<cstdlib>
#include<map>
#define mod 1000000007
#pragma warning(disable:4996)
using namespace std;
long long sum;
int s[1000008];
map<int, int>mp;
int main()
{
int n, m;
int i, j;
int t;
while (scanf("%d", &n) != EOF)
{
int x;
mp.clear();
for (i = 1; i <= n; i++)
{

scanf("%d", &s[i]);
if (!mp.count(s[i]))
mp[s[i]] = 1;
else
mp[s[i]]++;
}
for (map<int, int>::iterator i = mp.begin(); i != mp.end(); i++)
{
if (i->second >= ((n + 1) / 2)) 
{
cout << i->first << endl;
break;
}
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值