2489: C++结构体之统计最高最低分
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 20 Solved: 2
[ Submit][ Status][ Web Board]
Description
输入学生的姓名和成绩,统计出最高分的学生和最低分的学生。
Input
输入5个学生的姓名和分数,用结构体完成
Output
输出最高分的姓名和分数,最低分的姓名和分数
Sample Input
wang 98.8 li 67.9 zhang 56 lu 78.7 liu 70
Sample Output
MAX: wang 98.8 MIN:zhang 56
做题不但要注重算法,更应注意输出格式,就因为一个中文:错了好几遍。#include <iostream> #include <string> using namespace std; struct student{ string name; float score; } stu[5]; int main() { int i,j,k; for(i=0;i<5;i++) { cin>>stu[i].name>>stu[i].score; } int min; for(j=0;j<4;j++) { min=j; for(k=j+1;k<5;k++) if(stu[k].score<stu[min].score) min=k; swap(stu[j].score,stu[min].score); swap(stu[j].name,stu[min].name); } cout<<"MAX: "<<stu[4].name<<" "<<stu[4].score<<endl; cout<<endl; cout<<"MIN:"<<stu[0].name<<' '<<stu[0].score<<endl; //min后的:是中文 return 0; }