0
点赞
收藏
分享

微信扫一扫

杭电1872(稳定排序)sort的用法


杭电1872(稳定排序)sort的用法

这题考的应该是sort函数的用法吧
唯一的坑点就是要加id号进去排序,就是它的输入次序,否则怎么都是WA(翻评论区才知道的!哭嘞,一直WA,就是过不了)

#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<algorithm>
using namespace std;
/*
杭电1872稳定排序
*/
struct student
{
string name; //考生姓名
int id; //考生输入时的i值
int score; //考生成绩
};
int n;
student s[305];
bool cmp(student a,student b) //重载排序机制
{
if(a.score==b.score) return a.id<b.id; //成绩相同时,id升序排序
return a.score>b.score; //按成绩降序排序
}
void print() //打印排序结果
{
for(int i=0;i<n;i++)
{
cout<<s[i].name<<" "<<s[i].score<<endl;
}
}
int main()
{
bool flag;
int first;
student r[305]; //题目中它的排序结果
while(cin>>n)
{
flag=false;
first=9999;
for(int i=0;i<n;i++) //输入考生成绩
{
cin>>s[i].name>>s[i].score;
s[i].id=i; //获取它当前输入的次序,即id号
getchar();
}
sort(s,s+n,cmp); //排序
for(int i=0;i<n;i++)
{ //输入它的排序结果
cin>>r[i].name>>r[i].score;
getchar();
if(r[i].score>first)
{
flag=true; //如果出现升序,则是错误的排序方法
}
first=r[i].score;
}
if(flag)
{
cout<<"Error"<<endl;
print();
}
else
{
bool ans=false;
for(int i=0;i<n;i++)
{
if(s[i].name.compare(r[i].name)!=0)
{//如果它的排序方法和我的不同,则认为是不稳定的排序
ans=true;
break;
}
}
if(ans)
{
cout<<"Not Stable"<<endl;
print();
}
else cout<<"Right"<<endl;
}
}
return 0;
}


举报

相关推荐

0 条评论