基础题,结构体排序。
首先读入所有选手的解题数量和用时,将用时转换为秒。
然后排序,解题数量不同时,解题数量多的排在前面;解题数量相同时,用时少的排在前面。
最后根据金银铜牌数量和选手所在名次决定输出。
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
const int MAXN = 135;
struct participant //参赛选手
{
int solve; //解题数量
int time; //用时
int num; //序号
}p[MAXN];
//排序条件,解题数量不同时,解题数量多的排在前面
//解题数量相同时,用时少的排在前面
bool cmp(participant p1, participant p2)
{
if (p1.solve != p2.solve)
return p1.solve > p2.solve;
else
return p1.time < p2.time;
}
int main()
{
int N, G, S, C, M;
while (cin >> N >> G >> S >> C >> M)
{
if (N == 0 && G == 0 && S == 0 && C == 0 && M == 0)
break;
string str; //时间字符串
int h, m, s; //时,分,秒
for (int i = 1; i <= N; i++)
{
cin >> p[i].solve >> str;
h = (str[0] - '0') * 10 + (str[1] - '0');
m = (str[3] - '0') * 10 + (str[4] - '0');
s = (str[6] - '0') * 10 + (str[7] - '0');
p[i].time = h * 3600 + m * 60 + s;
p[i].num = i;
}
sort(p + 1, p + N + 1, cmp); //排序
for (int i = 1; i <= N; i++)
{
if (p[i].num == M)
{
if (i <= G)
cout << "Accepted today? I've got a golden medal :)" << endl;
else if (i <= G + S)
cout << "Accepted today? I've got a silver medal :)" << endl;
else if (i <= G + S + C)
cout << "Accepted today? I've got a copper medal :)" << endl;
else
cout << "Accepted today? I've got an honor mentioned :)" << endl;
break;
}
}
}
return 0;
}
继续加油。
来源:CSDN
作者:Intelligence1028
链接:https://blog.csdn.net/Intelligence1028/article/details/104561814