1028.List Sorting

来源:互联网 发布:淘宝手机客户端装修 编辑:程序博客网 时间:2024/06/08 05:15
【题意】
        给出一些学生的ID、姓名、等级,按照某一列排序并输出结果。

【思路】
        按题意直接码

【注意点】

        用string和cin会超时,得用char*和scanf


#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <vector>#include <algorithm>using namespace std;typedef struct{int id;char name[9];int grade;}record;bool cmpId(record r1, record r2){return r1.id<r2.id;}bool cmpName(record r1, record r2){if(strcmp(r1.name,r2.name)==0){return cmpId(r1,r2);}else if(strcmp(r1.name,r2.name)<0){return 1;}else{return 0;}}bool cmpGrade(record r1, record r2){if(r1.grade==r2.grade){return cmpId(r1,r2);}else{return r1.grade<r2.grade;}}int main(int argc, char const *argv[]){int n,c;vector<record> records;record tmp;cin >> n >> c;while(n--){scanf("%d%s%d", &tmp.id, tmp.name, &tmp.grade);records.push_back(tmp);}switch(c){case 1:sort(records.begin(),records.end(),cmpId);break;case 2:sort(records.begin(),records.end(),cmpName);break;case 3:sort(records.begin(),records.end(),cmpGrade);break;default:break;}for (vector<record>::iterator it=records.begin(); it!=records.end(); it++){printf("%06d %s %d\n", (*it).id, (*it).name, (*it).grade);}system("pause");return 0;}


0 0
原创粉丝点击