zoukankan      html  css  js  c++  java
  • 每日一题-1

    给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌。前三名运动员将会被分别授予 “金牌”,“银牌” 和“ 铜牌”("Gold Medal", "Silver Medal", "Bronze Medal")。

    (注:分数越高的选手,排名越靠前。)

    示例 1:

    输入: [5, 4, 3, 2, 1]
    输出: ["Gold Medal", "Silver Medal", "Bronze Medal", "4", "5"]
    解释: 前三名运动员的成绩为前三高的,因此将会分别被授予 “金牌”,“银牌”和“铜牌” ("Gold Medal", "Silver Medal" and "Bronze Medal").
    余下的两名运动员,我们只需要通过他们的成绩计算将其相对名次即可。
    

    Ans

    class Solution {
    public:
        vector<string> findRelativeRanks(const vector<int>& nums) {
            vector<int> index(nums.size());
            vector<string> ans(nums.size());
            iota(index.begin(), index.end(), 0);
            sort(index.begin(), index.end(), [&nums](const int& i, const int& j){
                return nums[j] < nums[i];
            });
            if (0 < nums.size()) ans[index[0]] = "Gold Medal";
            if (1 < nums.size()) ans[index[1]] = "Silver Medal";
            if (2 < nums.size()) ans[index[2]] = "Bronze Medal";
            for (int i = 3; i < nums.size(); ++i)
                ans[index[i]] = to_string(i + 1);
            return ans;
        }
    };
    
  • 相关阅读:
    好多天没写了,郁闷
    昨天很受教育
    恼火的服务器
    欢迎访问我的博客网站
    体育产品论坛
    参考书目
    web2.0与数字标准
    用户产生内容与网站做内容
    Using x++ code reading data from csv file format
    Find out specified the folder for all the files
  • 原文地址:https://www.cnblogs.com/Weber-security/p/12688763.html
Copyright © 2011-2022 走看看