具体的一些细节放在注释里面了,都挺弱智的细节,但是容易忽视,下次别做弱智
#include<iostream>
#include<algorithm> //为什么用它就不用多说了吧,你都用了algorithm,不就是sort函数啊
#include<string> //注意啊,你用了string类型,要用这个啊,只要涉及到字符串,你用用准没错
using namespace std;
const int N =1000010;
struct Student
{
Student(){}
int chinese,math;
string name; //在这里需要注意一些小细节,就是这个位置输入的是一串字符串,如果选择用char则必须每个单词后面跟一个‘/0’
}stu[N];
bool cnm(Student a,Student b)
{
if(a.chinese !=b.chinese)
{
return a.chinese>b.chinese;
}
else
{
return a.name < b.name;
}
}
bool cnm2(Student a,Student b)
{
if(a.math !=b.math)
{
return a.math>b.math;
}
else
{
return a.name < b.name;
}
}
int main()
{
int number =0,choose =0;
cin>>number>>choose;
for(int i =0;i<number;i++)
{
cin>>stu[i].name>>stu[i].chinese>>stu[i].math; //仔细敲一敲,别躲敲多了点(.)
}
if(choose ==1)
{
sort(stu,stu+number,cnm);
}
else{
sort(stu,stu+number,cnm2);
}
for(int i =0;i<number;i++)
{
cout<<stu[i].name<<" "<<stu[i].chinese<<" "<<stu[i].math<<endl;//注意细节,中间需要输出空格哟!
}
return 0;
}