这道题 需要多重判断 需要做标记 需要好好思考 没事就再纸上先写写过程
#include <stdio.h>
#include <stdlib.h>
struct Bang
{
int l;
int w;
};
int comp(const void *a,const void *b)
{
Bang *c=(Bang *)a;
Bang *d=(Bang *)b;
if(c->l!=d->l)
return c->l-d->l;
else
return c->w-d->w;//当长度相同时 重量也是要排序的
}
int main()
{
int T;
Bang s[5002];
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
int i,j,t,count=0;
for(i=0;i<n;i++)
scanf("%d%d",&s[i].l,&s[i].w);
qsort(s,n,sizeof(s[0]),comp);
for(i=0;i<n;i++)//一定要到n 不然无法遍历所有情况
{
t=s[i].w;
if(s[i].w!=0)
{
count++;
for(j=i+1;j<n;j++)
{
if(s[j].w!=0)
{
if(t<=s[j].w)//一定要有等于号 因为题中是大于等于的时候就可以少消耗一次
{
t=s[j].w;
s[j].w=0;
}
}
}
}
}
printf("%d\n",count);
}
return 0;
}