简单排序题,最后一个点估计是输入超时,以后再改。最后一个改成用scanf和printf后就过了。另外,sort的仿函数返回值要用bool,不要用int,不然会出现意想不到的情况。
//21:20 #include<stdio.h> #include<string.h> #include<stdlib.h> #include<algorithm> using namespace std; const int NUM=100005; struct student { char id[8]; char name[10]; int grade; }stu[NUM]; bool cmp_id(const student &a,const student &b) { return strcmp(a.id,b.id)<0; } bool cmp_name(const student &a,const student &b) { if(strcmp(a.name,b.name)==0) return strcmp(a.id,b.id)<0; return strcmp(a.name,b.name)<0; } bool cmp_grade(student a,student b) { if(a.grade==b.grade) return strcmp(a.id,b.id)<0; return a.grade < b.grade; } int main() { int n,c; //freopen("C:\\Documents and Settings\\Administrator\\桌面\\input.txt","r",stdin); scanf("%d%d",&n,&c); int i; for(i=0;i<n;i++){ scanf("%s%s%d",stu[i].id,stu[i].name,&stu[i].grade); } switch(c){ case 1: sort(stu,stu+n,cmp_id);break; case 2: sort(stu,stu+n,cmp_name); break; case 3: sort(stu,stu+n,cmp_grade); break; } for(i=0;i<n;i++){ printf("%s %s %d\n",stu[i].id,stu[i].name,stu[i].grade); } return 0; }