水题。。不会函数重载。。写了三个cmp。。 #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace std; struct type { char name[105]; int year; int price; }; int cmp1(struct type a,struct type b) { if(strcmp(a.name,b.name)==0) { if(a.year==b.year) return a.price<b.price; return a.year<b.year; } if(strcmp(a.name,b.name)<0) return 1; return 0; } int cmp2(struct type a,struct type b) { if(a.year==b.year) { if(strcmp(a.name,b.name)==0) return a.price<b.price; if(strcmp(a.name,b.name)<0) return 1; return 0; } return a.year<b.year; } int cmp3(struct type a,struct type b) { if(a.price==b.price) { if(strcmp(a.name,b.name)==0) return a.year<b.year; if(strcmp(a.name,b.name)<0) return 1; return 0; } return a.price<b.price; } int main(void) { int ncase; char tmp[10]; struct type book[105]; int blank = 0; while(cin>>ncase && ncase) { if(blank++) printf("/n"); for(int i=0;i<ncase;i++) cin>>book[i].name>>book[i].year>>book[i].price; cin>>tmp; if(strcmp(tmp,"Name")==0) { sort(book,book+ncase,cmp1); } else if(strcmp(tmp,"Year")==0) { sort(book,book+ncase,cmp2); } else { sort(book,book+ncase,cmp3); } for(int i=0;i<ncase;i++) { cout<<book[i].name<<" "<<book[i].year<<" "<<book[i].price<<endl; } } // cout << "Hello world!" << endl; return 0; }