本文主要是介绍1028 List Sorting(25分),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目翻译:
按照三种方式对结构体数组排序
题解思路:
模拟即可
代码:
#include<bits/stdc++.h>
using namespace std;
int N;struct student
{string ID, name;int grade;
};bool comp1(student s1, student s2)
{return s1.ID < s2.ID;
}bool comp2(student s1, student s2)
{if (s1.name < s2.name)return true;else if (s1.name == s2.name)return s1.ID < s2.ID;elsereturn false;
}bool comp3(student s1, student s2)
{if (s1.grade < s2.grade)return true;else if (s1.grade == s2.grade)return s1.ID < s2.ID;elsereturn false;
}int main()
{int M;cin >> N >> M;vector<student> students;for (int i = 0;i < N;i++){student temp;cin >> temp.ID >> temp.name >> temp.grade;students.push_back(temp);}switch (M){case 1:sort(students.begin(), students.end(), comp1);break;case 2:sort(students.begin(), students.end(), comp2);break;case 3:sort(students.begin(), students.end(), comp3);break;}for (auto i : students)cout << i.ID << " " << i.name << " " << i.grade << endl;
}
坑点:
无
这篇关于1028 List Sorting(25分)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!