保存输入,筛选出要输出的,进行排序即可
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int p[5];//保存题目的满分成绩
int N, K, M;
struct node {
vector<int> grade;//保存各科成绩
int id;
int rank;//排名
int sum;//总分
int P_num;//满分成绩数
bool is_show;//是否进行输出
node() { is_show = false; sum = 0;P_num = 0;grade.assign(K, -1); }
bool operator<(const node that) const {//按题目要求排序
return sum>that.sum ||
(sum == that.sum && P_num>that.P_num) ||
(sum == that.sum && P_num == that.P_num && id < that.id);
}
};
vector<node> f;//所有的输入
vector<node> r;//要输出的学生
int main()
{
cin >> N >> K >> M;
for (int t = 0;t < K;t++)
cin >> p[t];
f.resize(N);
while (M--)//保存输入
{
int a, b, c;
cin >> a >> b >> c;
f[a-1].id = a;
if (c == -1 && f[a - 1].grade[b - 1] == -1) f[a - 1].grade[b - 1] = 0;
if (c > f[a - 1].grade[b - 1])
{
f[a - 1].is_show = true;
if (c == p[b - 1]) f[a - 1].P_num++;
if (f[a - 1].grade[b - 1] == -1) {
f[a - 1].sum += c;f[a - 1].grade[b - 1] = c;
}
else
{
f[a - 1].sum += c- f[a - 1].grade[b - 1];f[a - 1].grade[b - 1] = c;
}
}
}
for (auto x : f)//对输入进行筛选
if (x.is_show == true)
r.push_back(x);
sort(r.begin(), r.end());
r[0].rank = 1;//成绩排名编号
for (int t = 2;t <= r.size();t++)
if (r[t - 1].sum == r[t - 2].sum) r[t - 1].rank = r[t - 2].rank;
else r[t - 1].rank = t;
for (auto x : r)//输出
{
printf("%d %05d %d",x.rank,x.id,x.sum);
for (auto y : x.grade)
if (y == -1) printf(" -");
else printf(" %d", y);
cout << endl;
}
}