题解 | #成绩排序#

成绩排序

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

import java.util.*;
public class Main {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            while (sc.hasNextLine()) {
                int n = Integer.parseInt(sc.nextLine());
                int u = Integer.parseInt(sc.nextLine());
                List<Stu> list = new ArrayList<Stu>();
                for (int i =0; i< n; i++) {
                    String[] arr = sc.nextLine().split(" ");
                    list.add(new Stu(arr[0], Integer.parseInt(arr[1])));
                }
                if (u == 0) {
                    Collections.sort(list, new Comparator<Stu>() {
                        @Override
                        public int compare(Stu s1, Stu s2) {
                            return s2.score - s1.score;
                        }
                    });
                } else if (u == 1) {
                    Collections.sort(list, new Comparator<Stu>() {
                        @Override
                        public int compare(Stu s1, Stu s2) {
                            return s1.score - s2.score;
                        }
                    });
                }
                for(int i =0; i< n; i++) {
                    Stu s = list.get(i);
                    System.out.println(s.name + " " + s.score);
                }
            }
        }
}
class Stu {
    String name;
    int score;
    public Stu(String name, int score) {
        this.name = name;
        this.score = score;
    }
}
全部评论

相关推荐

喜欢走神的孤勇者练习时长两年半:爱华,信华,等华,黑华
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务