您的位置:首页 > 其它

POJ 1065 (贪心)

2010-11-21 16:14 288 查看
1、qsort函数的应用

int cmp(const void *a, const void *b)
{
 struct data *c = (struct data *)a; //注意struct不能省!
 struct data *d = (struct data *)b; //!
 if((c->length) == (d->length))
  return c->weight - d->weight;
 else
  return c->length - d->length;
}

 

 

//c中竟然不能用bool?
#include <stdio.h>
#include <stdlib.h>
#define csMax 5001

struct data
{
int length;
int weight;
int flag;
};

struct data sticks[csMax]; //c中struct不能省,c++中可以!

int cmp(const void *a, const void *b)
{
struct data *c = (struct data *)a; //注意struct不能省!
struct data *d = (struct data *)b; //!
if((c->length) == (d->length))
return c->weight - d->weight;
else
return c->length - d->length;
}

int main()
{
int i, j;
int tCase, nNums;
int mixT, tempW;

while(1==scanf("%d", &tCase))
{
while(tCase --)
{
scanf("%d", &nNums);
for(i = 0; i < nNums; i++)
{
scanf("%d %d", &sticks[i].length, &sticks[i].weight);
sticks[i].flag=0;
}

qsort(sticks, nNums, sizeof(sticks[0]), cmp);

mixT = 0;
for(i = 0; i < nNums; i ++)
{
if(!sticks[i].flag)
{
mixT ++;
tempW = sticks[i].weight;
for(j = i + 1; j < nNums; j ++)
{
if((!sticks[j].flag) && (sticks[j].weight >= tempW))
{
sticks[j].flag = 1;
tempW = sticks[j].weight;
}
}
}
}
printf("%d/n", mixT);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  struct c