杭电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;
} 展开
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;
} 展开
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;
}
#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
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询