//虽然是一道水题,但是需要考虑的方面也挺多的,最开始没有考虑到需要进行三级排序:名字>年份>价格 //考虑到三级排序之后又没有考虑到格式,哎,编程真的很需要细腻的心思!所以WA了几次!下次需要注意了! #include "iostream" #include "vector" #include "string" #include "algorithm" using namespace std; struct Info//存储书的信息 { string name; int year; int price; }; //重载sort的比较符号,每一个的比较函数中都需要考虑三级排序! bool mycomp1(Info a, Info b) { if (a.name == b.name) { if (a.year == b.year) return a.price < b.price; else return a.year < b.year ; } else return a.name < b.name; } bool mycomp2(Info a, Info b) { if (a.price == b.price) { if (a.name == b.name) return a.year < b.year; else return a.name < b.name; } else return a.price < b.price; } bool mycomp3(Info a, Info b) { if (a.year == b.year) { if (a.name == b.name) return a.price < b.price; else return a.name < b.name; } else return a.year < b.year; } int main() { int Num; int F = 0; while (cin >> Num && Num != 0) { if (F) cout << endl;//格式输出的控制! Info *a = new Info[Num]; string flag; vector<Info> v; vector<Info>::iterator it; for (int i = 0; i < Num; i++) { cin >> a[i].name >> a[i].year >> a[i].price; v.push_back(a[i]); } cin >> flag; if (flag == "Year") sort(v.begin(), v.end(), mycomp3); else if (flag == "Price") sort(v.begin(), v.end(), mycomp2); else if (flag == "Name") sort(v.begin(), v.end(), mycomp1); for (it = v.begin(); it != v.end(); it++) cout << it->name << " " << it->year << " " << it->price << endl; F++; } }