poj2092

本文探讨了一个看似简单但实则容易混淆的问题——如何在数组中找到出现频率第二高的数字。通过实例分析,解释了初始变量初始化错误导致的误解,并提供了正确的解决方案。

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

简单题,但题意不容易理解,是求出现频度第二多的数字。

wa的原因是开始给vis数组初始话为0,而有些就该是0,却当成了没被赋值的用。

ContractedBlock.gifExpandedBlockStart.gifView Code
#include <iostream>
#include
<cstdio>
#include
<cstdlib>
#include
<cstring>
#include
<algorithm>
using namespace std;

#define maxn 10008

struct XPlayer
{
int id, p;
} xplayer[maxn
* 500];

int m, n;
int vis[maxn];
int pcount;

void input()
{
pcount
= 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
{
int a;
scanf(
"%d", &a);
if (vis[a] != -1)
xplayer[vis[a]].p
++;
else
{
xplayer[pcount].id
= a;
xplayer[pcount].p
= 1;
vis[a]
= pcount;
pcount
++;
}
}
}

bool operator <(const XPlayer &a, const XPlayer &b)
{
if (a.p == b.p)
return a.id < b.id;
return a.p > b.p;
}

int main()
{
//freopen("t.txt", "r", stdin);
while (scanf("%d%d", &n, &m), m | n)
{
memset(vis,
-1, sizeof(vis));
input();
sort(xplayer, xplayer
+ pcount);
int i = 1;
int temp = xplayer[1].p;
while (xplayer[i].p == temp && i < pcount)
{
printf(
"%d ", xplayer[i].id);
i
++;
}
printf(
"\n");
}
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值