AcWing 429. 奖学金
原题链接
简单
作者:
Fish丶
,
2021-01-17 11:22:43
,
所有人可见
,
阅读 370
方法一
用结构体的方法
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct Person
{
int chinese;
int score;
int num;
}arr[35];
bool cmp(struct Person& a, struct Person& b)
{
if (a.score > b.score) return true;
else if (a.score == b.score && a.chinese > b.chinese) return true;
else if (a.score == b.score && a.chinese == b.chinese && a.num < b.num) return true;
return false;
}
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
int a, b, c;
cin >> a >> b >> c;
int sum = a + b + c;
arr[i].num = i + 1;
arr[i].chinese = a;
arr[i].score = sum;
}
sort(arr, arr + n, cmp);
for (int i = 0; i < 5; i++)
cout << arr[i].num << " " << arr[i].score << endl;
}
方法二
用vector存三元素的方法(pair套pair)
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 35;
typedef pair<int, int> PII;
vector<pair<int, pair<int, int>>> v;
bool cmp(pair<int, PII> a, pair<int, PII> b)
{
if (a.second.second > b.second.second) return true;
else if (a.second.second == b.second.second && a.second.first > b.second.first)
return true;
else if (a.second.second == b.second.second && a.second.first == b.second.first && a.first < b.first)
return true;
return false;
}
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
int a, b, c;
cin >> a >> b >> c;
int sum = a + b + c;
v.push_back({ i + 1, {a, sum} });
}
sort(v.begin(), v.end(), cmp);
for (auto i = 0; i < 5; i++)
cout << v[i].first << " " << v[i].second.second << endl;
return 0;
}