1007 奥运排序问题

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&&copy[j].a2<=copy[j].a3&&copy[j].a2<=copy[j].a4)
            {
                copy[j].paiming=copy[j].a2;
                copy[j].res=2;
            }
            else if(copy[j].a3<k&&copy[j].a3<copy[j].a2&&copy[j].a3<=copy[j].a4)
            {
                copy[j].paiming=copy[j].a3;
                copy[j].res=3;
            }
            else if(copy[j].a4<k&&copy[j].a4<copy[j].a2&&copy[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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值