王道机试 2.1排序

#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
struct E{
    char name[100];
    int age;
    int score;
}buf[1000];//!!!!!!在此处定义
bool cmp(E a,E b)
{
    if(a.score!=b.score)
    {
        return a.score<b.score;//小的在前
    }
    int tmp=strcmp(a.name, b.name);
    if (tmp!=0){
        return tmp<0;
    }
    else
        return a.age<b.age;
}
int main(){
    int n;
    //int buf[1000]; 不是在这申明 而是在结构体后紧跟着
    while(scanf("%d",&n)!=EOF){
        for(int i=0;i<n;i++){
            scanf("%s%d%d",buf[i].name,&buf[i].age,&buf[i].score);//%s%d之间不需要空格
        }
        sort(buf,buf+n,cmp);
        for(int i=0;i<n;i++){
            printf("%s %d %d\n",buf[i].name,buf[i].age,buf[i].score);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Decmxj1229/article/details/87996419