PAT--1028. List Sorting
2016-07-28 15:57
375 查看
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; }
相关文章推荐
- 在命令行用 sort 进行排序
- Linux Shell - 如何使用sort与uniq命令删除重复的文本行
- Redis sort 排序命令详解
- 批处理命令教学之字符串排序(sort)
- mysql Sort aborted: Out of sort memory, consider increasing server sort buffer size的解决方法
- C++ 关于STL中sort()对struct排序的方法
- C#中Arraylist的sort函数用法实例分析
- Js中sort()方法的用法
- js采用concat和sort将N个数组拼接起来的方法
- 如何写JS数组sort的比较函数
- javascript中sort()的用法实例分析
- 详解Matlab中 sort 函数用法
- js模拟实现Array的sort方法
- Java数据结构及算法实例:冒泡排序 Bubble Sort
- js表数据排序 sort table data
- javascript 数组排序函数sort和reverse使用介绍
- JavaScript Table排序 2.0 (更新)
- js中数组(Array)的排序(sort)注意事项说明
- Javascript数组的排序 sort()方法和reverse()方法
- Java数据结构及算法实例:插入排序 Insertion Sort