題目要求如下:
代碼如下:
/*
* Copyright (c) 2013, 煙臺大學計算機學院
* All rights reserved.
* 作 者: 沈遠宏
* 完成日期:2014 年06月18日
* 版 本 號:v1.0
* 問題描述:Description
輸入學生的姓名和成績,統計出最高分的學生和最低分的學生。
Input
輸入5個學生的姓名和分數,用結構體完成
Output
輸出最高分的姓名和分數,最低分的姓名和分數
*/
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
struct Student
{
string name;
double score;
};
int main()
{
Student s1[5];
int max_index,min_index;
freopen("9.txt","r",stdin);
for(int i=0;i<5;++i)
{
cin>>s1[i].name>>s1[i].score;
}
double max_score=0,min_score=100;
for(int j=0;j<5;++j)
{
if(max_score<s1[j].score)
{
max_score=s1[j].score;
max_index=j;
}
if(min_score>s1[j].score)
{
min_score=s1[j].score;
min_index=j;
}
}
cout<<"MAX: "<<s1[max_index].name<<" "<<max_score<<endl;
cout<<endl;
cout<<"MIN:"<<s1[min_index].name<<" "<<min_score<<endl;
return 0;
}
運行結果:
OJ要求結果輸出例樣: