写这道题的时候输出一直没有Joe的名字
原因是char型数组存放字符串会在最后添加一个结束符’\0’,即空字符NULL,用以printf输出时识别,因此开字符数组的时候千万要记得字符数组的长度一定要比实际存储字符串的长度至少多1!
解决方法:
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
struct student{
char id[10]; //改为char id[11];
char name[10]; //改为char name[11];
int score;
}stu[200];
bool cmp(student a,student b){
return a.score>b.score;
}
int main(){
int n;
cin>>n;
for(int i=0;i<n;i++){
cin>>stu[i].name>>stu[i].id>>stu[i].score;
}
sort(stu,stu+n,cmp);
cout<<stu[0].name<<" "<<stu[0].id<<endl;
cout<<stu[n-1].name<<" "<<stu[n-1].id;
}