我的代码始终通不过-,-
/*
成绩查找+排序
cin:人数+排序方式+姓名+成绩
cout:姓名+成绩
*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Stu{
char name[10];
int score;
};
bool cmp(Stu a,Stu b){
return a.score > b.score;
}
bool cmp1(Stu a,Stu b){
return a.score < b.score;
}
int main(){
int n,type;
while(cin>>n>>type){
vector<Stu> v(n);
v.clear();
for(int i=0;i<n;i++){
cin>>v[i].name>>v[i].score;
}
if(type == 0){
sort(v.begin(),v.begin()+n,cmp);
}else if(type == 1){
sort(v.begin(),v.begin()+n,cmp1);
}
for(int i=0;i<n;i++){
cout<<v[i].name<<" "<<v[i].score<<endl;
}
}
return 0;
}
实在找不出问题!!在网上找到了和我的答案及其相似的解法,他的可以通过,还是不知道我的为什么?
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
struct student{
string name;
int score;
};
bool cmp0(const student &a, const student &b){
// 从高到低排序
return a.score > b.score;
}
bool cmp1(const student &a, const student &b){
// 从低到高排序
return a.score < b.score;
}
int main(){
int N, type;
while(cin >> N >> type){
vector<student> stud(N);
for(int i = 0; i < N; i ++){
cin >> stud[i].name >> stud[i].score;
}
if(type == 0)
stable_sort(stud.begin(), stud.end(), cmp0);
else
stable_sort(stud.begin(), stud.end(), cmp1);
for(int i = 0; i < N; i ++){
cout << stud[i].name << " " << stud[i].score << endl;
}
}
return 0;
}