解题思路

这是一个简单的排序问题,需要注意以下几点:

  1. 需要保存学生的姓名和成绩
  2. 排序时需要考虑相同成绩的情况(保持原有顺序)
  3. 根据输入的排序方式(0或1)决定是升序还是降序

主要步骤:

  1. 创建学生类存储姓名和成绩
  2. 使用稳定的排序算法(如稳定排序)
  3. 根据排序标志决定比较方式

代码

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

struct Student {
    string name;
    int score;
    int index;  // 记录原始顺序
    
    Student(string n, int s, int i) : name(n), score(s), index(i) {}
};

int main() {
    int n, order;
    while (cin >> n >> order) {
        vector<Student> students;
        string name;
        int score;
        
        // 读取学生信息
        for (int i = 0; i < n; i++) {
            cin >> name >> score;
            students.emplace_back(name, score, i);
        }
        
        // 排序
        if (order == 0) {  // 从高到低
            stable_sort(students.begin(), students.end(), 
                [](const Student& a, const Student& b) {
                    return a.score > b.score;
                });
        } else {  // 从低到高
            stable_sort(students.begin(), students.end(), 
                [](const Student& a, const Student& b) {
                    return a.score < b.score;
                });
        }
        
        // 输出结果
        for (const auto& student : students) {
            cout << student.name << " " << student.score << endl;
        }
    }
    return 0;
}
import java.util.*;

public class Main {
    static class Student {
        String name;
        int score;
        int index;
        
        public Student(String name, int score, int index) {
            this.name = name;
            this.score = score;
            this.index = index;
        }
    }
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int n = sc.nextInt();
            int order = sc.nextInt();
            
            List<Student> students = new ArrayList<>();
            
            // 读取学生信息
            for (int i = 0; i < n; i++) {
                String name = sc.next();
                int score = sc.nextInt();
                students.add(new Student(name, score, i));
            }
            
            // 排序
            if (order == 0) {  // 从高到低
                Collections.sort(students, (a, b) -> {
                    if (a.score != b.score) {
                        return b.score - a.score;
                    }
                    return a.index - b.index;
                });
            } else {  // 从低到高
                Collections.sort(students, (a, b) -> {
                    if (a.score != b.score) {
                        return a.score - b.score;
                    }
                    return a.index - b.index;
                });
            }
            
            // 输出结果
            for (Student student : students) {
                System.out.println(student.name + " " + student.score);
            }
        }
    }
}
class Student:
    def __init__(self, name, score, index):
        self.name = name
        self.score = score
        self.index = index

while True:
    try:
        n = int(input())
        order = int(input())
        
        students = []
        # 读取学生信息
        for i in range(n):
            name, score = input().split()
            students.append(Student(name, int(score), i))
        
        # 排序
        if order == 0:  # 从高到低
            students.sort(key=lambda x: (-x.score, x.index))
        else:  # 从低到高
            students.sort(key=lambda x: (x.score, x.index))
        
        # 输出结果
        for student in students:
            print(f"{student.name} {student.score}")
            
    except:
        break

算法及复杂度

  • 算法:稳定排序
  • 时间复杂度:,使用了排序算法
  • 空间复杂度:,需要存储所有学生信息