PAT--1028. List Sorting

pat 1028

题解

简单排下序。

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

class Student{
    String ID;
    String name;
    int    score;
    public Student(String iD, String name, int score) {
        super();
        ID = iD;
        this.name = name;
        this.score = score;
    }
}

class SortWay implements Comparator<Student>{
    int way;
    SortWay(int c){
        way = c;
    }

    @Override
    public int compare(Student o1, Student o2) {
        if(way == 1){
            return o1.ID.compareTo(o2.ID);
        }
        else if(way == 2){
            if(!o1.name.equals(o2.name)) return o1.name.compareTo(o2.name);
            else return o1.ID.compareTo(o2.ID);
        }
        else{
            if(o1.score != o2.score) return o1.score - o2.score;
            else return o1.ID.compareTo(o2.ID);
        }
    }

}


public class Main{

    public static void main(String[] args) {

        ArrayList<Student> arr = new ArrayList<Student>();

        Scanner in = new Scanner(System.in);
        int n = in.nextInt(), c = in.nextInt();

        for(int i = 0; i < n; ++i){
            arr.add(new Student(in.next(), in.next(), in.nextInt()));
        }

        Collections.sort(arr, new SortWay(c));

        for(Student stu : arr){
            System.out.println(stu.ID + " " + stu.name + " " + stu.score);
        }

    }
}

效率是硬伤。

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

const int maxn = 100000 + 10;
struct Student{
    string ID;
    string name;
    int score;

    Student(){}
    Student(string id, string n, int s):ID(id), name(n), score(s){}
};

int   n, c;
Student stu[maxn];

int main()
{
#ifdef LOCAL
freopen("data.in", "r", stdin);
#endif // LOCAL

    cin >> n >> c;
    char id[10], name[10];
    int score;

    for(int i = 0; i < n; ++i){
//        cin >> stu[i].ID >> stu[i].name >> stu[i].score;
        scanf("%s %s %d", id, name, &score);
        stu[i] = Student(id, name, score);
    }

    if(c == 1){
        sort(stu, stu + n, [](Student& s1, Student& s2){ return s1.ID < s2.ID; });
    }
    else if(c == 2){
        sort(stu, stu + n, [](Student& s1, Student& s2){
                if(s1.name != s2.name) return s1.name < s2.name;
                else return s1.ID < s2.ID;
             });
    }
    else{
        sort(stu, stu + n, [](Student& s1, Student& s2){
                if(s1.score != s2.score) return s1.score < s2.score;
                else return s1.ID < s2.ID;
             });
    }
    for(int i = 0; i < n; ++i){
//        cout << stu[i].ID << " " << stu[i].name << " " << stu[i].score << endl;
        printf("%s %s %d\n", stu[i].ID.c_str(), stu[i].name.c_str(), stu[i].score);
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值