hdu 1862 EXCEL排序

题目链接(请点击)
思路:这个题并不难,主要是把,怎样排序的函数写好就行(cmp1,cmp2,cmp3)。题目中当C==2时要求按照姓名的非递减字典序排序,所以自己想着如果用string定义姓名name这个字符串,可以直接进行字母比较,故用string。

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
struct Student
{
    int id,grade;
    string name;
}stu[100000];
int cmp1(Student a,Student b)
{
    return a.id<b.id;
}
int cmp2(Student a,Student b)
{
    if(a.name!=b.name)
        return a.name<b.name;
    else return a.id<b.id;
}
int cmp3(Student a,Student b)
{
    if(a.grade!=b.grade)
        return a.grade<b.grade;
    else return a.id<b.id;
}
int main()
{
    int N,C,ans=1;
    while(cin>>N>>C&&N)
    {
        for(int i=0;i<N;i++)
            cin>>stu[i].id>>stu[i].name>>stu[i].grade;
        if(C==1) sort(stu,stu+N,cmp1);
        else if(C==2) sort(stu,stu+N,cmp2);
        else sort(stu,stu+N,cmp3);
        cout<<"Case "<<ans<<":"<<endl;
        for(int i=0;i<N;i++)
        {
            printf("%06d ",stu[i].id);
            cout<<stu[i].name<<" "<<stu[i].grade<<endl;
        }
        ans++;
    }
    return 0;
}

需要注意的是 string不能用printf输出会报错。printf不能输出string类定义的字符串,因为string是扩展的一个类,而并非内部的,不能直接用printf输出。原本自己在输出时全部都用的printf(学号6位前面很可能需要补空格,就想着用printf直接就是%06d好一些,但是报错了,当然学号要是定义成字符串类型的就另当别论了)这是需要注意的。

猜你喜欢

转载自blog.csdn.net/thwwu/article/details/79980871