学生不会超过100个,不会出现两个学生的名字仅大小写有差别的情况 输出 格式和输入数据基本一样,唯一不同在于输出学号的时候,必须用前导0补足8位 样例输入 Tom Hanks 7863,M 18 Mary Lu 18343,F 21 Santa Fe 27863,M 17 样例输出 Mary Lu 00018343,F 21 Santa Fe 00027863,M 17 Tom Hanks 00007863,M 18
参考代码//http://poj.grids.cn/practice/3719/ //1.输入数据最后有可能有若干个回车,也有可能没有 //2.不会出现两个学生的名字仅大小写有差别的情况 //3.提示不可信,造就多次CE。(用 stricmp 函数作大小写无关的字符串比较。如果在POJ上交,名字要改成 _stricmp) //4.此题很简单,休息一下,仔细想想就明白了。 // //哥爱水水,主教级别TX不要BS哥。 //Accepted 256kB 0ms 1218 B G++ #include <iostream> #include <cstdio> #include <cstdlib> #include <iomanip> #include <cstring> #include <algorithm> using namespace std; //Student information typedef class Student{ public: char name[19]; char low_name[19]; int id; char sex; int age; void tolowername(char *s); }Student; Student stu[101]; void Student::tolowername(char *s){ int i,len = strlen(s); for(i = 0;i < len;++ i){ this->low_name[i] = tolower(s[i]); } this->low_name[i] = '\0'; } //compare int comp(const void *x,const void *y){ return strcmp(((Student*)x)->low_name, ((Student*)y)->low_name); } int main(){ Student pstu; int i,j; char ch; //initialize and input data i = 0; while(std::cin.getline(pstu.name,19)){ if(strcmp(pstu.name,"")==0) continue; std::cin>>pstu.id>>ch>>pstu.sex>>pstu.age; getchar(); pstu.tolowername(pstu.name); stu[i ++] = pstu; } //sorted by name asc qsort(stu,i,sizeof(Student),comp); //output result for(j = 0;j < i;++ j){ std::cout<<stu[j].name<<std::endl; std::cout<<std::setw(8)<<std::setfill('0')<<stu[j].id<<","<<(char)stu[j].sex<<" "<<stu[j].age<<std::endl; } return 0; }