您的位置:首页 > 其它

leetcode 506. Relative Ranks 运动员名次

2017-12-12 10:00 363 查看
Given scores of N athletes, find their relative ranks and the people with the top three highest scores, who will be awarded medals: “Gold Medal”, “Silver Medal” and “Bronze Medal”.

Example 1:

Input: [5, 4, 3, 2, 1]

Output: [“Gold Medal”, “Silver Medal”, “Bronze Medal”, “4”, “5”]

Explanation: The first three athletes got the top three highest scores, so they got “Gold Medal”, “Silver Medal” and “Bronze Medal”.

For the left two athletes, you just need to output their relative ranks according to their scores.

本题题意很简单,就是做一个简单的排序

代码如下:

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <climits>
#include <algorithm>
#include <sstream>
#include <functional>
#include <bitset>
#include <cmath>

using namespace std;

bool cmp(int a, int b)
{
return a > b;
}
class Solution
{
public:
vector<string> findRelativeRanks(vector<int>& nums)
{
vector<string> res;
vector<int> a = nums;
sort(a.begin(), a.end(),cmp);
for (int i : nums)
res.push_back(getRank(i, a));
return res;
}

string getRank(int target, vector<int> a)
{
int i = 0;
for (i = 0; i < a.size(); i++)
{
if (a[i] == target)
break;
}
if (i == 0)
return "Gold Medal";
else if (i == 1)
return "Silver Medal";
else if (i == 2)
return "Bronze Medal";
else
return to_string(i + 1);
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: