您的位置:首页 > 其它

POJ 3664 (qsort 快排)

2010-11-15 21:20 267 查看
//2010-11-15
#include <stdio.h>
#include <stdlib.h>

struct vote
{
int firstRound;
int secondRound;
int ID;
};

struct vote arr[50001];

int cmp(const void *a, const void *b)
{//必须这样写!用“->”错! 明白此处 “.” 和“ ->” 的区别!!!!
return (*(struct vote *)b).firstRound - (*(struct vote *)a).firstRound;
//	return ((struct vote *)b)->firstRound - ((struct vote *)a)->firstRound; 这样也是可以的
}
/* 1. 结构体变量.成员名
2. (* p).成员名 两侧的括号不能省,成员运算符.优先于 *
3. p->成员名
三者等价!
详见谭浩强 c语言程序设计第三版 p289-290
*/
int main()
{
int i;
int nCows, nFirstSelect;
int temp, index;

while(2==scanf("%d%d", &nCows, &nFirstSelect))
{
for(i=0; i<nCows; i++)
{
scanf("%d%d", &arr[i].firstRound, &arr[i].secondRound);
arr[i].ID=i+1;
}

qsort(arr, nCows, sizeof(arr[0]), cmp);

temp=0;
for(i=0; i<nFirstSelect; i++)
{
if(arr[i].secondRound>temp)
{
temp=arr[i].secondRound;
index=i;
}
}

printf("%d/n", arr[index].ID);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  struct 语言 2010 c