hoj 1744 Grandpa is Famoust

The whole family was excited by the news. Everyone knew grandpa had been an extremely good bridge player for decades, but when it was announced he would be in the Guinness Book of World Records as the most successful bridge player ever, whow, that was astonishing!

The International Bridge Association (IBA) has maintained, for several years, a weekly ranking of the best players in the world. Considering that each appearance in a weekly ranking constitutes a point for the player, grandpa was nominated the best player ever because he got the highest number of points.

Having many friends who were also competing against him, grandpa is extremely curious to know which player(s) took the second place. Since the IBA rankings are now available in the internet he turned to you for help. He needs a program which, when given a list of weekly rankings, finds out which player(s) got the second place according to the number of points.

Input

The input contains several test cases. Players are identified by integers from 1 to 10000. The first line of a test case contains two integers N and M indicating respectively the number of rankings available (2 ≤ N ≤ 500) and the number of players in each ranking (2 ≤ M ≤ 500). Each of the next N lines contains the description of one weekly ranking. Each description is composed by a sequence of M integers, separated by a blank space, identifying the players who figured in that weekly ranking. You can assume that:

  • in each test case there is exactly one best player and at least one second best player,
  • each weekly ranking consists of M distinct player identifiers.

The end of input is indicated by N = M = 0.

Output

For each test case in the input your program must produce one line of output, containing the identification number of the player who is second best in number of appearances in the rankings. If there is a tie for second best, print the identification numbers of all second best players in increasing order. Each identification number produced must be followed by a blank space.

Sample Input

4 5
20 33 25 32 99
32 86 99 25 10
20 99 10 33 86
19 33 74 99 32
3 6
2 34 67 36 79 93
100 38 21 76 91 85
32 23 85 31 88 1
0 0
Sample Output
32 33
1 2 21 23 31 32 34 36 38 67 76 79 88 91 93 100

题意很简单:爷爷打桥牌很厉害,拿得冠军。但是他还想知道,除了他,那些选手是第二(或者并列第二)。现已知每场比赛,有名次的选手的编号,请根据获得名次的次数的多少判定选手的排名,求第二(或者并列第二)的选手的编号(有并列情况时按ID号由小到大输出)。

由于博主比较蠢,想不出 啥好方法,就用了一个最笨的办法,先对所有 输入的数据进行排序(升序)。再用一个结构体存储每个数据以及其出现的频率( 排除掉所有重复的元素)再用一个辅助空间对每个数据的频率排序(降序),数组第二个元素即为所要找的key(获得名次的次数),然后再在结构体中查找key比较输出即可。

开始Runtime Error了无数次,感觉思路啥的没问题,但就是不能AC反复写了好几份代码有结构体数组的,有链表的还是不行。。最后搞毛了直接把数组开到十万的规模poj居然过了,然后跑到hoj依然Runtime Error,

好吧再开大点20万的规模,终于过了。。

两份AC代码都贴下,希望能够帮助一些人可怜

结构体数组实现:

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define Size 250100
int data[Size] = { 0 };
int buf[Size] = { 0 };
typedef struct player
{
	int count;
	int score;
}Play;
Play pep[Size];
// 快排的驱动函数 升序
int cmp(const void *a, const void *b)
{
	return *(int *)a - *(int *)b;
}
// 降序
int cmp1(const void *a, const void *b)
{
	if (*(int *)a == *(int *)b)
		return 0;
	return *(int *)a > *(int *)b ? -1 : 1;
}
void Insert_Elem(int n)
{
	if (1 == n)
	{
		printf("%d\n", data[0]);
	}
	else
	{
		int i, j, k = 0;
		memset(pep, 0, sizeof(pep));
		memset(buf, 0, sizeof(buf));
		// 在结构体数组中插入元素排除重复的元素
		for (i = 0; i < n; i++)
		{
			int flag = 1;
			for (j = 0; j <= k; j++)
			{
				if (pep[j].score == data[i])
				{
					flag = 0;
				}
			}
			if (flag)
				pep[k++].score = data[i];
		}
		// 在原始数据中统计每个数据出现的频度
		for (i = 0; i < n; i++)
		{
			for (j = 0; j < k; j++)
			{
				if (pep[j].score == data[i])
					pep[j].count++;
			}
		}
		// 对数据的频度进行排序 降序
		for (i = 0; i < k; i++)
		{
			buf[j++] = pep[i].count;
		}
		qsort(buf, j, sizeof(buf[0]), cmp1);
		// 查找 输出
		for (i = 0; i < k; i++)
		{
			if (pep[i].count == buf[1])
				printf("%d ", pep[i].score);
		}
		printf("\n");
	}
}
int main()
{
	int i,j, n, m;
	while (scanf("%d %d", &n, &m) != EOF && 0 != n && 0 != m)
	{
		int k = 0;
		memset(data, 0, sizeof(data));
		for (i = 0; i < n; i++)
		{
			for (j = 0; j < m; j++)
				scanf("%d", &data[k++]);
		}
		qsort(data, n*m, sizeof(data[0]), cmp);
		Insert_Elem(n * m);
	}
	return 0;
}
链表实现:

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define Size 250100
int data[Size] = { 0 };
int buf[Size] = { 0 };
typedef struct menber
{
	int count;
	int score;
	struct menber *pNext;
}Node, *pNode;
int cmp(const void *a, const void *b)
{
	return *(int *)a - *(int *)b;
}
int cmp1(const void *a, const void *b)
{
	if (*(int *)a == *(int *)b)
		return 0;
	return *(int *)a > *(int *)b ? -1 : 1;
}
pNode Add_New_Node(int score)
{
	pNode NewNode = (pNode)malloc(sizeof(Node));
	NewNode->score = score;
	NewNode->count = 1;
	NewNode->pNext = NULL;
	return NewNode;
}
void Create_List(pNode *pHead, int score)
{
	if (*pHead == NULL)
	{
		*pHead = Add_New_Node(score);
	}
	else
	{
		int flag = 1;
		pNode p = *pHead, p1 = NULL;
		while (p != NULL)
		{
			p1 = p;
			if (p->score == score)
			{
				p->count++;
				flag = 0;
			}
			p = p->pNext;
		}
		if (flag)
			p1->pNext = Add_New_Node(score);
	}
}
void Result(pNode pHead)
{
	pNode p = pHead; int j = 0;
	memset(buf, 0, sizeof(buf));
	while (p != NULL)
	{
		buf[j++] = p->count;
		p = p->pNext;
	}
	if (1 == j)
	{
		printf("%d\n", pHead->score);
	}
	else
	{
		qsort(buf, j, sizeof(buf[0]), cmp1);
		p = pHead;
		while (p != NULL)
		{
			if (p->count == buf[1])
				printf("%d ", p->score);
			p = p->pNext;
		}
		printf("\n");
	}
}
void FreeAll(pNode *pHead)
{
	pNode p1,p = *pHead;
	p1 = p->pNext;
	while (p1!= NULL)
	{
		p = p1;
		p1 = p1->pNext;
		free(p);
	}
	free(*pHead);
}
int main()
{
	int i, j, n, m;
	while (scanf("%d %d", &n, &m) != EOF && 0 != n && 0 != m)
	{
		int k = 0;
		pNode Head = NULL;
		memset(data, 0, sizeof(data));
		for (i = 0; i < n; i++)
		{
			for (j = 0; j < m; j++)
				scanf("%d", &data[k++]);
		}
		qsort(data, n * m, sizeof(data[0]), cmp);
		for (i = 0; i < n * m; i++)
		{
			Create_List(&Head, data[i]);
		}
		Result(Head);
		FreeAll(&Head);
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值