简单解法 | 成绩排序,欢迎讨论和提问。

成绩排序

https://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //处理输入
        int n=sc.nextInt();
        int op=sc.nextInt();sc.nextLine();//消耗缓存区的换行符
        String[] name=new String[101];//存这个分数对应的名字
        Arrays.fill(name,"");
        for(int i=0;i<n;i++){
            String str=sc.nextLine();
            String[] m=str.split(" ");
            int s=Integer.parseInt(m[1]);
            name[s]+=(m[0]+" "); 
        }
        //ArrayList存储score
        List<Integer> score=new ArrayList<>();
        for(int i=0;i<name.length;i++){
            if(name[i]!=""){
                score.add(i);
            }
        }
        //ArrayList转数组
        Integer[] scores=score.toArray(new Integer[score.size()]);
        //排序
        if(op==1)
            Arrays.sort(scores);//从小到大
        else
            Arrays.sort(scores,Collections.reverseOrder());//倒序
        //开始输出
        for(int i=0;i<scores.length;i++){
            if(name[scores[i]]!=""){//此分数存在人
                String[] ans=name[scores[i]].split(" ");
                for(int j=0;j<ans.length;j++){
                    System.out.println(ans[j]+" "+scores[i]);
                }
            }
                
            
        }
    }
    
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
11-21 11:29
已编辑
斯卡蒂味的鱼汤:知道你不会来数马,就不捞你😂最近数马疯狂扩招,招聘要求挺低的,你能力肯定够,应该就是因为太强了,知道你不会来才不捞你
投递腾讯云智研发等公司8个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务