题解 | #成绩排序#

成绩排序

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

import java.io.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(bf.readLine());
        int index = Integer.parseInt(bf.readLine());
        String[] name = new String[n];
        int[] score = new int[n];

        for (int i = 0; i < n; i++) {
            String[] temp = bf.readLine().trim().split(" ");
            name[i] = temp[0];
            score[i] = Integer.parseInt(temp[1]);
        }

        boolean swapped;
        do {
            swapped = false;
            for (int j = 0; j < n -1; j++) {
                if ((index == 1 && score[j] > score[j + 1]) || (index == 0 && score[j] < score[j + 1])) {
                    swapped = true;
                    String na = name[j];
                    name[j] = name[j + 1];
                    name[j + 1] = na;
                    
                    int t = score[j];
                    score[j] = score[j + 1];
                    score[j + 1] = t;
                }
            }
        } while (swapped);

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; i++) {
            sb.append(name[i] + " ");
            sb.append(score[i]);
            sb.append("\n");
        }

        System.out.println(sb.toString());
    }
}

2个数组pai'xu

全部评论

相关推荐

AFBUFYGRFHJLP:直接去美帝试试看全奖phd吧
点赞 评论 收藏
分享
喜欢走神的孤勇者练习时长两年半:爱华,信华,等华,黑华
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务