注意点:用cin和cout最后一个点会超时。
printf输出string:
string a = “abcde”;
printf("%s",a.c_str());
scanf输入string:
string a;
a.resize(100); //需要预先设置大小
scanf("%s",&a[0]);
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct Student{
string id;
string name;
int score;
};
vector<Student> students;
bool cmp1(Student a,Student b){
return a.id < b.id;
}
bool cmp2(Student a,Student b){
if(a.name != b.name) return a.name < b.name;
else return a.id < b.id;
}
bool cmp3(Student a,Student b){
if(a.score != b.score) return a.score < b.score;
else return a.id < b.id;
}
int main(){
int n,c;
scanf("%d %d",&n,&c);
for(int i = 0;i < n;i++){
Student temp;
temp.id.resize(10);
temp.name.resize(10);
scanf("%s %s %d",&temp.id[0],&temp.name[0],&temp.score);
students.push_back(temp);
}
switch(c){
case 1:
sort(students.begin(),students.end(),cmp1);
break;
case 2:
sort(students.begin(),students.end(),cmp2);
break;
case 3:
sort(students.begin(),students.end(),cmp3);
break;
}
for(int i = 0;i < n-1;i++){
printf("%s %s %d\n",students[i].id.c_str(),students[i].name.c_str(),students[i].score);
}
printf("%s %s %d",students[n - 1].id.c_str(),students[n - 1].name.c_str(),students[n - 1].score);
system("pause");
return 0;
}