杭电acm1862 EXCEL排序的实现

来源:互联网 发布:软件企业即征即退期限 编辑:程序博客网 时间:2024/05/23 01:46

本人比较菜,至今还没有搞懂字典是怎么回事
查了大佬的结题报告,改成了C++语言
仔细看看是能够都明白的,就是简单的排序问题,被字典整蒙了。。。

#include<iostream>#include <cstdio>  #include <string.h>  #include <algorithm> #define maxn 100005using namespace std;  struct node  {      char id[20];      char name[20];      int fen;  } a[maxn];  bool cmp1(node x,node y)  {      if( strcmp(x.id , y.id) < 0 )//学号递增排序          return true;      else          return false;  }  bool cmp2(node x,node y)  {      if(strcmp(x.name,y.name) < 0 )//名字字典序非递减排序          return true;      else if(strcmp(x.name , y.name) == 0)      {          if( strcmp(x.id , y.id) < 0 )//当若干学生具有相同姓名时,则按他们的学号递增排序              return true;      }      return false;  }  bool cmp3(node x,node y)  {      if(x.fen<y.fen) return true;//成绩非递减排序      else if(x.fen==y.fen)      {          if( strcmp(x.id , y.id) < 0 )//当若干学生具有相同成绩时,则按他们的学号递增排序              return true;      }      return false;  }  int main()  {      int n,c,T=0,i;      while(cin>>n>>c&&n!=0)      {          for(i=0; i<n; i++)              cin>>a[i].id>>a[i].name>>a[i].fen;          switch(c)          {          case 1:              sort(a,a+n,cmp1);              break;          case 2:              sort(a,a+n,cmp2);              break;          case 3:              sort(a,a+n,cmp3);            break;          }          cout<<"Case "<<++T<<":"<<endl;          for(i=0; i<n; i++)              cout<<a[i].id<<" "<<a[i].name<<" "<<a[i].fen<<endl;      }      return 0;  }
原创粉丝点击