方法1:用数组下标来表示成绩,输出时,按照stu[maxgrade]输出即可:
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
struct Student{
char name[12];
char number[12];
}stu[105];
int main(){
int n;
scanf("%d",&n);
int grade;
char name[12];
char number[12];
int maxnumber=0,minnumber=100;
for(int i=0;i<n;i++){
scanf("%s %s %d",name,number,&grade);
strcpy(stu[grade].name,name);
strcpy(stu[grade].number,number);
maxnumber = max(grade,maxnumber);
minnumber = min(grade,minnumber);
}
printf("%s %s\n",stu[maxnumber].name,stu[maxnumber].number);
printf("%s %s",stu[minnumber].name,stu[minnumber].number);
return 0;
}
方法二:
这个方法更好
#include<stdio.h>
#include<string.h>
using namespace std;
struct student{
char name[15];
char id[15];
int score;
}temp,ans_max,ans_min;
int main(){
int n;
scanf("%d",&n);
ans_max.score = 0;
ans_min.score = 101;
for(int i=0;i<n;i++){
scanf("%s%s%d",temp.name,temp.id,&temp.score);
if(temp.score > ans_max.score) ans_max = temp;
if(temp.score < ans_min.score) ans_min = temp;
}
printf("%s %s\n",ans_max.name,ans_max.id);
printf("%s %s",ans_min.name,ans_min.id);
return 0;
}
京公网安备 11010502036488号