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;
}
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; }
相关文章推荐
- poj -1065 Wooden Sticks (贪心or dp)
- POJ 1065 Wooden Sticks(贪心,)
- POJ 1065 Wooden Sticks 贪心
- HDU_1051 && POJ_1065 Wooden Sticks(贪心)
- POJ 1065 贪心
- POJ 1065 Wooden Sticks / hdu 1257 最少拦截系统 DP 贪心
- POJ 1065 贪心 最少的上升子序列个数
- POJ-1065-Wooden Sticks-贪心-多关键字排序
- poj 1065 贪心 或 dp
- POJ - 1065 Wooden Sticks DP(LIS)|| 贪心
- POJ 1065 Wooden Sticks (贪心)
- POJ 1065 Wooden Sticks (贪心)
- poj 1065 Wooden Sticks 【贪心 新思维】
- poj 1065_贪心
- POJ 1065 && HDU 1051 Wooden Sticks 贪心
- POJ 1065 Wooden Sticks(贪心 LIS思想)
- poj 1065简单的贪心
- poj 1065 Wooden Sticks【贪心】
- POJ 1065 Wooden Sticks【贪心】
- Poj 1065 Wooden Sticks【贪心+LIS?】