题目概述:
读入 n(>0)名学生的姓名、学号、成绩,分别输出成绩最高
和成绩最低学生的姓名和学号。
思路:
此题一出就想着用结构体
有许多细节需要注意,第一次自己写c++
需要注意
1.不应该使用printf输出一个String类型的字符串
2.用c++的排序时 针对的是结构体里的元素进行排序 所以需
要注意谓词的使用
3.还有题目中并没有说明存多少人,数组尽量开大一些
代码:
#include<iostream>
#include<string>
#include <algorithm>
using namespace std;
struct Student
{
string name;
string sno;
int grade;
};
Student student[10000];//出错时开大一些
bool compare(Student s1,Student s2)
{
return s1.grade<s2.grade;
}
int main(){
int n=0;//学生数
scanf("%d",&n);
for(int i=0;i<n;i++)
{
cin>>student[i].name;
cin>>student[i].sno;
cin>>student[i].grade;
}
sort(student,student+n,compare);
cout<<student[n-1].name<<" ";
cout<<student[n-1].sno<<endl;
cout<<student[0].name<<" ";
cout<<student[0].sno<<endl;
}