zoukankan      html  css  js  c++  java
  • 浙江大学研究生机试题——奥运排序问题

    分明是一道水题!!完全没有算法,模拟即可。

    然而在做这道题时无意中复习了一下关于函数对象的知识,所以这道题还是有意义的,另外对于C++不熟悉的话,

    写起来会比较烦。

    题目描述:

    按要求,给国家进行排名。

    输入:
    有多组数据。
    第一行给出国家数N,要求排名的国家数M,国家号从0到N-1。
    第二行开始的N行给定国家或地区的奥运金牌数,奖牌数,人口数(百万)。
    接下来一行给出M个国家号。
    输出:
    排序有4种方式: 金牌总数 奖牌总数 金牌人口比例 奖牌人口比例
    对每个国家给出最佳排名排名方式 和 最终排名
    格式为: 排名:排名方式
    如果有相同的最终排名,则输出排名方式最小的那种排名,对于排名方式,金牌总数 < 奖牌总数 < 金牌人口比例 < 奖牌人口比例
    如果有并列排名的情况,即如果出现金牌总数为 100,90,90,80.则排名为1,2,2,4.
    每组数据后加一个空行。
    样例输入:
    4 4
    4 8 1
    6 6 2
    4 8 2
    2 12 4
    0 1 2 3
    4 2
    8 10 1
    8 11 2
    8 12 3
    8 13 4
    0 3
    样例输出:
    1:3
    1:1
    2:1
    1:2
    
    1:1
    1:1
    
    思路就是把每个国家看成一个结构体,对结构体的每种特性进行分别排序,然后统计一下对于每个国家,在哪种
    排序中排名更靠前一些,注意,如果金牌数相同的100 90 90 80,排名是1,2,2,4;对于排名相同的两种排序方法,
    金牌总数 < 奖牌总数 < 金牌人口比例 < 奖牌人口比例,就是说,在以金牌数为key排序的过程中如果排名是1,
    在以奖牌数排序的过程中也是排名为1,那么就采用以金牌数为key的方式。

    若果强行写排序的话,就会出现写很多sort的情况,我们通过对sort的cmp函数写成函数对象的形式,可以实现按照
    不同的key排序;
    struct cmp
    {
        int comp;
        cmp(int x):comp(x){};
        bool operator() (const node &a,const node &b)const
        {
            if(comp==1) return a.c.g_md > b.c.g_md;
            else if(comp == 2) return a.c.md > b.c.md;
            else if(comp == 3) return a.c.pg > b.c.pg;
            else return a.c.pm > b.c.pm;
        }
    };
    
    

    在这里,cmp是一个重载了函数调用预算符号的类,如果我们生命一个对象:cmp a(1)以后,就可以把a传给sort函数,他就会自动的

    按照我们约定好的方式排序。具体讲解请看《C++primer 5th》重载运算与类型转换14.8节;

    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    const int inf = 1000000;
    const int maxn = 1e5 + 5;
    int n,m;
     
    struct country//国家
    {
        int g_md,md,y,peo;
        double pg,pm;
        country(){}
        country(int x,int y,int z):g_md(x),md(y),peo(z)
        {
            pg = g_md*1.0/peo;
            pm = md*1.0/peo;
        }
    };
    struct node//国家+编号
    {
        int id;
        country c;
        node(){}
        node(int i,country &rhs):id(i),c(rhs){}
    };
    struct Ans//第i个国家最优的结果和采用什么方式排序
    {
        int pos, kind;
        Ans(){pos = inf;}
        Ans(int x, int y):pos(x),kind(y){}
    };
    vector<country> vc;
    vector<node> v;
    vector<Ans> ans;//要输出的答案
    struct cmp
    {
        int comp;
        cmp(int x):comp(x){};
        bool operator() (const node &a,const node &b)const
        {
            if(comp==1) return a.c.g_md > b.c.g_md;
            else if(comp == 2) return a.c.md > b.c.md;
            else if(comp == 3) return a.c.pg > b.c.pg;
            else return a.c.pm > b.c.pm;
        }
    };
    bool equal(country &P,country &Q,int k)
    {
        if(k == 1) return P.g_md == Q.g_md;
        if(k == 2) return P.md == Q.md;
        if(k == 3) return P.pg == Q.pg;
        if(k == 4) return P.pm == Q.pm;
    }
    void update(vector<node>& v,int kind)//小心key值相同的元素要特殊处理
    {
        int ps = 1;
        if(ans[v[0].id].pos > ps)
            ans[v[0].id]=Ans(ps,kind);
         
        for(int i = 1; i < v.size(); ++i)
        {
            if(!equal(v[i-1].c,v[i].c,kind)) ps = i+1;
            if(ans[v[i].id].pos > ps)
                ans[v[i].id] = Ans(ps,kind);
        }
    }
    int main()
    {
        for(;cin >> n >> m;){
            int x, y, z;
            ans.resize(m);//重新设置vector的大小,resize函数
            for(int i = 0 ; i < n; ++i){
                cin >> x>> y>>z;
                vc.push_back(country(x,y,z));
            }
            int id;
            for(int i = 0; i < m; ++i){
                cin >> id;
                v.push_back(node(i,vc[id]));
            }
            for(int i = 1; i <= 4; ++i){
                sort(v.begin(),v.end(),cmp(i));
                update(v,i);
            }
            for(int i = 0; i < m; ++i)
            {
                cout << ans[i].pos <<':'<< ans[i].kind<<endl;
            }
            cout << endl;
            v.clear();
            vc.clear();
            ans.clear();
        }
    }
    
    
  • 相关阅读:
    Python 【第十三章】 Django 之 FORM
    Django-Model操作数据库(增删改查、连表结构)参考
    Python 【第十二章】 Django 之 Model 数据库操作
    Python 【第十一章】 Django模版
    Python 【第十章】 Django路由
    Ajax操作如何实现跨域请求 (JSONP和CORS实现Ajax跨域的原理)
    浏览器同源政策概述
    Python 【第九章】 Django基础
    python 2.7下的正则将中文分隔符去掉
    python time应用 之时间戳转换
  • 原文地址:https://www.cnblogs.com/Norlan/p/5370253.html
Copyright © 2011-2022 走看看