1排序函数 sort()函数用法
#include <algorithm>
4级排序
bool compare1(const struct Counrtry& a,const struct Counrtry& b)
{
if(a.godnum>b.godnum)
return true;
else if(a.godnum==b.godnum)
{
if(a.num>b.num)
return true;
else if(a.num==b.num)
{
if(a.god_num>b.god_num)
return true;
else if(a.god_num-b.god_num>-eps&&a.god_num-b.god_num<eps)//浮点数比较
{
return a.num_num>b.num_num;
}
return false;
}
return false;
}
return false;
}
sort(begin , end) 表示一个范围,默认升序 ,想要降序 ,自己写cmp函数哦
2.如果有并列排名的情况,即如果出现金牌总数为 100,90,90,80.则排名为1,2,2,4,测试用例肯定有这点
我的逗B代码:
#include <stdio.h>
#include <algorithm>
#include <iostream>
using namespace std;
#define eps 1e-8
struct Counrtry
{
int godnum;
int num;
int people;
double god_num;
double num_num;
int a1;
int a2;
int a3;
int a4;
int id;
int flag;//确定排名是否确定
int paiming;//排名
int res;//排序方式
};
bool compare1(const struct Counrtry& a,const struct Counrtry& b)
{
if(a.godnum>b.godnum)
return true;
else if(a.godnum==b.godnum)
{
if(a.num>b.num)
return true;
else if(a.num==b.num)
{
if(a.god_num>b.god_num)
return true;
else if(a.god_num-b.god_num>-eps&&a.god_num-b.god_num<eps)//浮点数比较
{
return a.num_num>b.num_num;
}
return false;
}
return false;
}
return false;
}
bool compare2(const struct Counrtry& a,const struct Counrtry& b)
{
if(a.num>b.num)
return true;
else if(a.num==b.num)
{
if(a.god_num>b.god_num)
return true;
else if(a.god_num-b.god_num>-eps&&a.god_num-b.god_num<eps)
{
return a.num_num>b.num_num;
}
return false;
}
return false;
}
bool compare3(const struct Counrtry& a,const struct Counrtry& b)
{
if(a.god_num>b.god_num)
return true;
else if(a.god_num-b.god_num>-eps&&a.god_num-b.god_num<eps)
{
return a.num_num>b.num_num;
}
return false;
}
bool compare4(const struct Counrtry& a,const struct Counrtry& b)
{
return a.num_num>b.num_num;
}
bool compare5(const struct Counrtry& a,const struct Counrtry& b)
{
return a.id<b.id;
}
int main()
{
int N,M;
int i,j;
int k;
int favorite[1024];
Counrtry cont[1024];
Counrtry copy[1024];
while(scanf("%d %d",&N,&M)!=EOF)
{
for(i=0; i<N; i++)
{
scanf("%d %d %d",&cont[i].godnum,&cont[i].num,&cont[i].people);
cont[i].god_num=(double)cont[i].godnum/(double)cont[i].people;
cont[i].num_num=(double)cont[i].num/(double)cont[i].people;
cont[i].id=i;
}
for(j=0; j<M; j++)
{
scanf("%d",&favorite[j]);
copy[j]=cont[favorite[j]];
}
//cout<<"After****************************!\n";
//for(j=0; j<M; j++)
//{
//cout<<"->number "<<copy[j].id<<" "<<copy[j].godnum<<" "<<copy[j].num<<" "<<copy[j].people<<endl;
//}
//cout<<"1111111111111111111111111111111\n";
sort(copy,copy+M,compare1);
for(j=0; j<M; j++)
{
//cout<<"->number "<<copy[j].id<<" "<<copy[j].godnum<<" "<<copy[j].num<<" "<<copy[j].people<<endl;
if(j==0) copy[j].a1=1;
else if(copy[j].godnum<copy[j-1].godnum) copy[j].a1=j+1;//copy[j].a1=copy[j-1].a1+1;
else if(copy[j].godnum==copy[j-1].godnum) copy[j].a1=copy[j-1].a1;
}
//cout<<"222222222222222222222222222222222\n";
sort(copy,copy+M,compare2);
for(j=0; j<M; j++)
{
//cout<<"->number "<<copy[j].id<<" "<<copy[j].godnum<<" "<<copy[j].num<<" "<<copy[j].people<<endl;
if(j==0) copy[j].a2=1;
else if(copy[j].num<copy[j-1].num) copy[j].a2=j+1;
else if(copy[j].num==copy[j-1].num) copy[j].a2=copy[j-1].a2;
}
//cout<<"333333333333333333333333333333333\n";
sort(copy,copy+M,compare3);
for(j=0; j<M; j++)
{
//cout<<"->number "<<copy[j].id<<" "<<copy[j].godnum<<" "<<copy[j].num<<" "<<copy[j].people<<endl;
if(j==0) copy[j].a3=1;
else if(copy[j].god_num<copy[j-1].god_num) copy[j].a3=j+1;
else if(copy[j].god_num==copy[j-1].god_num) copy[j].a3=copy[j-1].a3;
}
//cout<<"44444444444444444444444444444444444\n";
sort(copy,copy+M,compare4);
for(j=0; j<M; j++)
{
//cout<<"->number "<<copy[j].id<<" "<<copy[j].godnum<<" "<<copy[j].num<<" "<<copy[j].people<<endl;
if(j==0) copy[j].a4=1;
else if(copy[j].num_num<copy[j-1].num_num) copy[j].a4=j+1;
else if(copy[j].num_num==copy[j-1].num_num) copy[j].a4=copy[j-1].a4;
}
//cout<<"*************************RES********************\n";
sort(copy,copy+M,compare5);//id升序排序
for(j=0; j<M; j++)
{
//cout<<"->number "<<copy[j].id<<" a1 "<<copy[j].a1<<" a2 "<<copy[j].a2<<" a3 "<<copy[j].a3<<" a4 "<<copy[j].a4<<endl;
k=copy[j].a1;
if(k<=copy[j].a2&&k<=copy[j].a3&&k<=copy[j].a4)
{
copy[j].paiming=k;
copy[j].res=1;
}
else if(copy[j].a2<k&©[j].a2<=copy[j].a3&©[j].a2<=copy[j].a4)
{
copy[j].paiming=copy[j].a2;
copy[j].res=2;
}
else if(copy[j].a3<k&©[j].a3<copy[j].a2&©[j].a3<=copy[j].a4)
{
copy[j].paiming=copy[j].a3;
copy[j].res=3;
}
else if(copy[j].a4<k&©[j].a4<copy[j].a2&©[j].a4<copy[j].a3)
{
copy[j].paiming=copy[j].a4;
copy[j].res=4;
}
cout<<copy[j].paiming<<":"<<copy[j].res<<endl;
}
printf("\n");
}
return 0;
}