杭电acm1050题,一直wa求大牛们帮忙!!!

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1050#include<stdio.h>#include<iostream... 题目连接:
http://acm.hdu.edu.cn/showproblem.php?pid=1050#include<stdio.h>
#include<iostream>
#include<stdlib.h>
using namespace std;
struct room
{
int s;
int t;
}rooms[210];
int cmp( const void *a , const void *b )
{
struct room *c =(room *)a;
struct room *d =(room *)b;
if(c->s!=d->s)
return c->s-d->s;
else
return c->t-d->t;
}

int main()
{
int i,j,n,sum,m,ri,repeat,temp;
cin>>repeat;
for(ri=0;ri<repeat;ri++)
{
sum=0;
cin>>n;
for(i=0;i<n;i++)
{
cin>>rooms[i].s>>rooms[i].t;
if(rooms[i].s>rooms[i].t)
{
temp=rooms[i].s;
rooms[i].s=rooms[i].t;
rooms[i].t=temp;
}
}

qsort(rooms,n,sizeof(rooms[0]),cmp);
j=0;
for(i=1;i<n;i++)
{
if((rooms[j].t+1==rooms[i].s&&rooms[j].t%2!=0)||(rooms[j].t>rooms[i].s))
{
sum++;
j=i;
}
}
cout<<(sum+1)*10<<endl;
}
return 0;
}
展开
 我来答
porker2008
2012-05-30 · TA获得超过1.4万个赞
知道大有可为答主
回答量:7066
采纳率:62%
帮助的人:1.1亿
展开全部
#include<stdio.h>
#include<iostream>
#include<stdlib.h>
using namespace std;
struct room
{
int s;
int t;
bool selected;
}rooms[210];
int cmp( const void *a , const void *b )
{
struct room *c =(room *)a;
struct room *d =(room *)b;
if(c->s!=d->s)
return c->s-d->s;
else
return c->t-d->t;
}

int main()
{
int i,j,n,sum,m,ri,repeat,temp;
cin>>repeat;
for(ri=0;ri<repeat;ri++)
{
sum=0;
cin>>n;
for(i=0;i<n;i++)
{
cin>>rooms[i].s>>rooms[i].t;
rooms[i].selected = false;
if(rooms[i].s>rooms[i].t)
{
temp=rooms[i].s;
rooms[i].s=rooms[i].t;
rooms[i].t=temp;
}
}

qsort(rooms,n,sizeof(rooms[0]),cmp);
while(1)
{
j=0;
while(j<n && rooms[j].selected) j++;
if(j==n) break;
sum++;
rooms[j].selected = true;
for(i=j+1;i<n;i++)
{
if(rooms[i].selected) continue;
if(!((rooms[j].t+1==rooms[i].s&&rooms[j].t%2!=0)||(rooms[j].t>=rooms[i].s)))
{
rooms[i].selected = true;
j=i;
}
}
}
cout<<sum*10<<endl;
}
return 0;
}
追问
额,谢谢大神,但还是想不通自己那样做为什么是错的?跪求指导
追答
是扫一遍是不行的
比如说你可以试一下
4
10 20
20 30
30 40
40 50
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式