class Solution {
public String[] findRelativeRanks(int[] score) {
int n = score.length;
String[] str1 = {"Gold Medal","Silver Medal","Bronze Medal"};
int[][] arr = new int[n][2];// n行 2列 的二维数组
for(int i = 0;i < n;i++){
arr[i][0] = score[i];//将运动员得分存入二维数组每一行一维数组的0下标位置
arr[i][1] = i;// //将运动员编号存入二维数组每一行一维数组的1下标位置
}
Arrays.sort(arr,(a,b)->b[0]-a[0]);
String[] str2 = new String[n];// 记录 每个运动的名次
for(int i = 0;i < n;i++ ){
if(i<3){// 由于 arr 是 降序,所以前面三名运动员,是 第 1 ~ 第3名
str2[arr[i][1]] = str1[i];
}else{
str2[arr[i][1]] = Integer.toString(i+1);
}
}
return str2;
}
}
import java.util.*;
class Solution {
public String[] findRelativeRanks(int[] score) {
int n = score.length;
String[] str1 = {"Gold Medal","Silver Medal","Bronze Medal"};
int[][] arr = new int[n][2];
for (int i = 0; i < n; i++) {
arr[i][0] = score[i];
arr[i][1] = i;
}
Arrays.sort(arr,(a,b)->b[0] - a[0]);
String[] str2 = new String[n];
for (int i = 0; i < n; i++) {
if(i<3){
str2[arr[i][1]] = str1[i];
}else{
str2[arr[i][1]] = Integer.toString(i+1);
}
}
return str2;
}
}
public class Manuscript{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] score = new int[sc.nextInt()];
int n = score.length;
int i = 0;
while(n--!=0 && sc.hasNextInt()){
score[i++] = sc.nextInt();
}
Solution solution = new Solution();
String[] str = solution.findRelativeRanks(score);
System.out.println(Arrays.toString(str));
sc.close();
}
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/DarkAndGrey/article/details/121889394
内容来源于网络,如有侵权,请联系作者删除!