pat 1012

题目没说清楚等分数的情况怎么排名,我以为不用考虑--遂改,同等分数一个名次,否则名次就是分数比其高的人数+1。用了结构指针 数组,还有一个模板化的比较函数,模板参数是一个int,代表对应的ACME。开一个函数指针数组,放入四个具现化的comp。然后调用sort四次,每次把排名填入。ac- 

 1 #include <iostream>
 2 #include <vector>
 3 #include <string>
 4 #include <algorithm>
 5 #include <memory>
 6 #include <map>
 7 
 8 using namespace std;
 9 
10 struct stu {
11     double scores[4];
12     int rank[4];//A C M E
13     string id;
14     stu(const string& s, double c, double m, double e) :id(s)
15     {
16         scores[0] = (c + m + e) / 3;
17         scores[1] = c, scores[2] = m, scores[3] = e;
18     }
19 };
20 
21 typedef bool(*comp)(shared_ptr<stu>&, shared_ptr<stu>&);
22 template<int i>
23 bool com(shared_ptr<stu>&lhs, shared_ptr<stu>& rhs)
24 {
25     return lhs->scores[i] > rhs->scores[i];
26 }
27 
28 pair<int, int> getpair(shared_ptr<stu>& ss)
29 {
30     int best_rank = (1<<30);
31     int best_sub = -1;
32     for (int i = 0;i < 4;i++)
33         if (ss->rank[i] < best_rank)
34         {
35             best_rank = ss->rank[i];
36             best_sub = i;
37         }
38     return make_pair(best_sub, best_rank);
39 }
40 int main(void)
41 {
42     vector<comp> com_vec;
43     //for(int i=0;i<4;i++)
44     com_vec.push_back(com<0>);
45     com_vec.push_back(com<1>);
46     com_vec.push_back(com<2>);
47     com_vec.push_back(com<3>);
48     
49 
50     const string subject = "ACME";
51     int n, m;
52     cin >> n >> m;
53     vector<shared_ptr<stu> > sv;
54     sv.reserve(n);
55     for (int i = 0;i < n;i++)
56     {
57         string s;
58         double c, m, e;
59         cin >> s >> c >> m >> e;
60         sv.push_back(shared_ptr<stu>(new stu(s, c, m, e)));
61     }
62     //sort 4 times
63     for (int i = 0;i < 4;i++)
64     {
65         sort(sv.begin(), sv.end(), com_vec[i]);
66         for (int j = 0;j < sv.size();j++)
67         {
68             //对应科目名次
69             sv[j]->rank[i] = j + 1;
70             if (j != 0 && sv[j]->scores[i] == sv[j - 1]->scores[i])
71                 sv[j]->rank[i] = sv[j - 1]->rank[i];
72             
73         }
74     }
75     typedef int sub;
76     typedef int ran;
77     map<string, pair<sub, ran> > ma;
78     for (int i = 0;i < sv.size();i++)
79     {
80         auto p = getpair(sv[i]);
81         //if (ma.find(sv[i]->id) == ma.end())
82             ma[sv[i]->id] = p;
83     }
84     //give ans
85     for (int i = 0;i < m;i++)
86     {
87         string temp;
88         cin >> temp;
89         auto it = ma.find(temp);
90         if (it != ma.end())cout << it->second.second
91             << " " << subject[it->second.first] << endl;
92         else cout << "N/A" << endl;
93     }
94 
95     return 0;
96 }
原文地址:https://www.cnblogs.com/schsb/p/8823248.html