杭电1501,请哪位高手看一下,测试数据都是对的。代码如下:

#include<stdio.h>#include<stdlib.h>#include<string.h>structstick{intlength,weight;};s... #include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct stick
{
int length,weight;
};

stick s[5005];
int vist[5005];

int cmp(const void* a,const void* b)
{
stick *fa = (stick *)a;
stick *fb = (stick *)b;
return fa->length-fb->length;
}

int main()
{
int v,test;
int i,j,a,b,n,num,count;
int weight[5005];
scanf("%d",&test);
for (v=0;v<test;v++)
{
j = 1;
memset(vist,0,sizeof(vist));
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d%d",&a,&b);
s[i].length = a;
s[i].weight = b;

}
qsort(s,n,sizeof(s[0]),cmp);
num = s[0].weight;
count = 0;
while(j>0)
{
j = 0;
for (i=0;i<n;i++)
{
if(!vist[i] && num<=s[i].weight)
{
vist[i] = 1;
num = s[i].weight;
}
if(!vist[i] && num>s[i].weight)
{
weight[j++] = s[i].weight;
}

}
num = weight[0];
count ++;
}
printf("%d\n",count);
}
return 0;
}
展开
 我来答
porker2008
2011-07-27 · TA获得超过1.4万个赞
知道大有可为答主
回答量:7066
采纳率:62%
帮助的人:1.1亿
展开全部
#include <stdio.h>
#include <胡蚂stdlib.h>
#include <string.h>

struct stick
{
int length,weight;
};

stick s[5005];
int vist[5005];

int cmp(const void* a,const void* b)
{
stick *fa = (stick *)a;
stick *fb = (stick *)b;
if(fa->length != fb->length) return fa->length-fb->length;
return fa->weight-fb->weight; // 如果铅腊长度相同裤激埋, 应当按照质量来排序
}

int main()
{
int v,test;
int i,j,a,b,n,num,count;
int weight[5005];
scanf("%d",&test);
for (v=0;v<test;v++)
{
j = 1;
memset(vist,0,sizeof(vist));
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d%d",&a,&b);
s[i].length = a;
s[i].weight = b;

}
qsort(s,n,sizeof(s[0]),cmp);
num = s[0].weight;
count = 0;
while(j>0)
{
j = 0;
for (i=0;i<n;i++)
{
if(!vist[i] && num<=s[i].weight)
{
vist[i] = 1;
num = s[i].weight;
}
if(!vist[i] && num>s[i].weight)
{
weight[j++] = s[i].weight;
}

}
num = weight[0];
count ++;
}
printf("%d\n",count);
}
return 0;
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式