1028 List Sorting (25)(友元函数的使用)
来源:互联网 发布:linux系统工程师培训 编辑:程序博客网 时间:2024/06/16 00:41
自己写的用超时。
以下为他人做的,不超时
http://blog.csdn.net/lv_zj/article/details/16114247
#include <string.h>
#include <algorithm>
#include <stdio.h>
class cstudent
{
public:
int id;
char name[10];
int score;
friend bool IdCmp(const cstudent &s1,const cstudent &s2);
friend bool NameCmp(const cstudent &s1,const cstudent &s2);
friend bool ScoreCmp(const cstudent &s1,const cstudent &s2);
};
bool IdCmp(const cstudent &s1,const cstudent &s2)
{
return s1.id<s2.id;
}
bool NameCmp(const cstudent &s1,const cstudent &s2)
{
int icmp=strcmp(s1.name,s2.name);
if(icmp==0) return s1.id<s2.id;
else return icmp<0;
}
bool ScoreCmp(const cstudent &s1,const cstudent &s2)
{
if(s1.score==s2.score) return s1.id<s2.id;
else return s1.score<s2.score;
}
class CA
{
public:
enum{N=100010};
void run();
cstudent vs[N];
};
void CA::run()
{
int n,k;
cstudent stu;
scanf("%d%d",&n,&k);
int i;
for(i=0;i<n;i++)
{
scanf("%d%s%d",&stu.id,stu.name,&stu.score);
vs[i]=stu;
}
if(k==1)
{
std::sort(vs,vs+n,IdCmp);
}
else if(k==2)
{
std::sort(vs,vs+n,NameCmp);
}
else if(k==3)
{
std::sort(vs,vs+n,ScoreCmp);
}
for(i=0;i<n;i++)
{
printf("%06d %s %d\n",vs[i].id,vs[i].name,vs[i].score);
}
}
int main()
{
// freopen("test.in","r",stdin);
CA *a=new CA;
a->run();
return 0;
}
参考2:
http://blog.csdn.net/sunbaigui/article/details/8657115
- 1028 List Sorting (25)(友元函数的使用)
- 1028 List Sorting (25)
- 友元函数的使用
- 友元函数的使用
- pat 1028 List Sorting
- pat 1028 List Sorting
- 1028 List Sorting
- PAT-A1028. List Sorting (25)(排序.)
- PAT(甲级)1028. List Sorting (25)
- 1028. List Sorting (25)
- 1028. List Sorting (25)
- 1028. List Sorting (25)
- 1028. List Sorting (25)
- 1028. List Sorting (25)
- 1028. List Sorting (25)
- 1028. List Sorting (25)
- 1028. List Sorting (25)
- 1028. List Sorting (25)
- 交换两个变量的多种方法
- 八皇后问题
- pat:1002(Advanced)
- 常见音频格式
- 并查集——Friendship
- 1028 List Sorting (25)(友元函数的使用)
- 【BZOJ 1975】 魔法猪学院
- 从阅读中寻找乐趣,在学习中不断成长
- hdu-1856 More is better
- mysql之查询优化探索
- cocos2dx 文件处理
- [UIImage imageNamed:@""]使用误区
- C++一些注意点之new和malloc
- C语言的内存分配(参考)