506. Relative Ranks

    xiaoxiao2021-04-12  48

    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.

    Note:

    N is a positive integer and won't exceed 10,000.All the scores of athletes are guaranteed to be unique.

    #include <iostream> #include <vector> #include <algorithm> #include <string> using namespace std; class Solution { public:     vector<string> findRelativeRanks(vector<int>& nums) {         vector<int>n = nums;         sort(n.begin(),n.end(),greater<int>());   //        for(int i=0;i<n.size();i++) //        cout << n[i] << " ";         vector<string>re; //        cout << nums.size(); for(int i=0;i<nums.size();i++) re.push_back("0");         if(nums.size() <= 3)         {         for(int i=0;i<nums.size();i++)         {         if(nums[i] == n[0]) re[i] = "Gold Medal"; else if(nums[i] == n[1]) re[i] = "Silver Medal"; else if(nums[i] == n[2]) re[i] = "Bronze Medal"; } }                  else if(nums.size() > 3)         {         for(int i=0;i<nums.size();i++) { if(nums[i] == n[0]) re[i] = "Gold Medal"; else if(nums[i] == n[1]) re[i] = "Silver Medal"; else if(nums[i] == n[2]) re[i] = "Bronze Medal"; else { for(int j=3;j<n.size();j++) { if(n[j] == nums[i]) { // char* s; // sprintf(s, "%d", j+1);  re[i] = to_string(j+1); } } } } return re;       } };

    转载请注明原文地址: https://ju.6miu.com/read-667148.html

    最新回复(0)