解题思路

这是一个排序和分类问题。具体要求:

  1. 每道题目包含:
    • 名称()
    • 提交次数()
    • 通过次数()
  2. 根据通过率()将题目分为3个难度等级:
    • :难度5
    • :难度4
    • :难度3
  3. 按题目名称字典序排序并输出

代码

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

class Question {
public:
    string name;
    int x, y;
    float passRate;
    int level;
    
    Question(string name, int x, int y) {
        this->name = name;
        this->x = x;
        this->y = y;
        this->passRate = (float)y / x;
        this->level = calculateLevel();
    }
    
    int calculateLevel() {
        if(passRate <= 0.3f) return 5;
        if(passRate <= 0.6f) return 4;
        return 3;
    }
};

int main() {
    int n;
    cin >> n;
    
    vector<Question> questions;
    string name;
    int x, y;
    
    // 读入题目信息
    for(int i = 0; i < n; i++) {
        cin >> name >> x >> y;
        questions.emplace_back(name, x, y);
    }
    
    // 按名称排序
    sort(questions.begin(), questions.end(), 
        [](const Question& a, const Question& b) {
            return a.name < b.name;
        });
    
    // 输出结果
    for(const auto& q : questions) {
        cout << q.name << " " << q.level << endl;
    }
    
    return 0;
}
import java.util.*;

class Question implements Comparable<Question> {
    String name;
    int x, y;
    double passRate;
    int level;
    
    public Question(String name, int x, int y) {
        this.name = name;
        this.x = x;
        this.y = y;
        this.passRate = (double)y / x;
        this.level = calculateLevel();
    }
    
    private int calculateLevel() {
        if(passRate <= 0.3) return 5;
        if(passRate <= 0.6) return 4;
        return 3;
    }
    
    @Override
    public int compareTo(Question other) {
        return this.name.compareTo(other.name);
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        
        List<Question> questions = new ArrayList<>();
        
        // 读入题目信息
        for(int i = 0; i < n; i++) {
            String name = sc.next();
            int x = sc.nextInt();
            int y = sc.nextInt();
            questions.add(new Question(name, x, y));
        }
        
        // 排序
        Collections.sort(questions);
        
        // 输出结果
        for(Question q : questions) {
            System.out.println(q.name + " " + q.level);
        }
    }
}
class Question:
    def __init__(self, name: str, x: int, y: int):
        self.name = name
        self.x = x
        self.y = y
        self.pass_rate = y / x
        self.level = self.calculate_level()
    
    def calculate_level(self) -> int:
        if self.pass_rate <= 0.3:
            return 5
        if self.pass_rate <= 0.6:
            return 4
        return 3

def main():
    n = int(input())
    questions = []
    
    # 读入题目信息
    for _ in range(n):
        name, x, y = input().split()
        questions.append(Question(name, int(x), int(y)))
    
    # 按名称排序
    questions.sort(key=lambda q: q.name)
    
    # 输出结果
    for q in questions:
        print(f"{q.name} {q.level}")

if __name__ == "__main__":
    main()

算法及复杂度

  • 算法:排序
  • 时间复杂度: - 主要是排序的时间复杂度
  • 空间复杂度: - 需要存储所有题目信息