Topic links: http://acm.hdu.edu.cn/showproblem.php?pid=1862
Problem-solving ideas: Sorting structure
#include <bits/stdc++.h> using namespace std; struct student { int no; string name; int grade; }stu[100005]; bool cmp1(student s1, student s2) { return s1.no < s2.no; } bool cmp2(student s1, student s2) { if (s1.name < s2.name) { return true; } else if (s1.name == s2.name) { return s1.no < s2.no; } return false; } bool cmp3(student s1, student s2) { if (s1.grade < s2.grade) { return true; } else if (s1.grade == s2.grade) { return s1.no < s2.no; } return false; } int main() { int n, c, cas = 1; while (cin >> n >> c, n) { for (int i = 0; i < n; i++) { cin >> stu[i].no >> stu[i].name >> stu[i].grade; } if (c == 1) { sort(stu, stu+n, cmp1); } else if (c == 2) { sort(stu, stu+n, cmp2); } else if (c == 3) { sort(stu, stu+n, cmp3); } printf("Case %d:\n", cas++); for (int i = 0; i < n; i++) { The printf ( " % S% D% 06D \ n- " , STU [I] .no, STU [I] .name.c_str (), STU [I] .grade); // .c_str () converts the string into the char [] } } return 0 ; }