成绩排序
成绩排序
http://www.nowcoder.com/questionTerminal/7a2f7d304d9e43b1bb2a6e72ed65bf51
大致思路就是按照题目要求设置一下sort函数的cmp参数
#include <iostream> #include <algorithm> #include <string> using namespace std; typedef struct { string name; int age; int grade; } Student; bool cmp(Student a, Student b) { if (a.grade < b.grade) { return true; } else if (a.grade > b.grade) { return false; } else { //the same grade if (a.name < b.name) { return true; } else if (a.name > b.name) { //the same name return false; } else { return a.age < b.age; } } } int main() { int N; cin>>N; Student *arr = new Student[N]; for (int i = 0; i < N; i++) { cin>>arr[i].name>>arr[i].age>>arr[i].grade; } sort(arr, arr+N, cmp); for (int i = 0; i < N; i++) { cout<<arr[i].name<<" "<<arr[i].age<<" "<<arr[i].grade<<endl; } return 0; }