Problem D: C++结构体之统计最高最低分

Time Limit: 1 Sec   Memory Limit: 128 MB

Submit: 6254  

Solved: 815

[

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

HINT


[ Submit][

Status][

Web Board]


한국어 中文 فارسی English ไทย
Anything about the Problems, Please Contact Admin:admin
All Copyright Reserved 2010-2014 HUSTOJ TEAM
GPL2.0 2003-2014 HUSTOJ Project TEAM
Help Maunal


实现代码 1

 

#include <iostream>
#include <algorithm>
using namespace std ;
typedef struct Student{
	char name[100] ;
	float score ;
	
}Stu;
struct relue{
	bool operator ()(const Stu &a ,const Stu &b)
    {
    	return a.score <b.score ;
    }
};
void solve(Stu *a ,int n)
{
	int i ;
	sort(a,a+n ,relue());

	cout<<"MAX: "<<a[n-1].name <<" "<<a[n-1].score<<endl;
	cout<<"MIN: "<<a[0].name <<" "<<a[0].score<<endl;
}
int main()
{
	int i ;
	int n =  5 ;
	Stu a[5] ;
	for(i = 0 ; i < n ;i++ )
	{
		cin >>a[i].name >>a[i].score ;
		
	}
	solve(a,n);
	
	
	return 0 ;
	
}


代码 2 

#include<iostream>
#include<iomanip>
using namespace std;
typedef struct student{
    char name[100] ;
    float scroe ;

}Stu;
const int n = 5 ;
int main()
{
    Stu a[n] ;
    int i ;
    float MAX=0, MIN=0 ;
    int flag1,flag2 ;
    for(i =0 ;i<n ;i++)
    {
        cin>>a[i].name>>a[i].scroe ;


    }
     MIN=a[0].scroe ;
        MIN=a[0].scroe ;
    for(i=0; i<n ;i++)
    {

             if(a[i].scroe>MAX)
        {
            MAX =a[i].scroe ;
            flag1 = i ;

        }
       if(a[i].scroe<MIN)
        {
            MIN =a[i].scroe ;
            flag2 =i ;
        }
    }

    cout <<"MAX: "<<a[flag1].name<<" "<<a[flag1].scroe<<endl;
    cout <<"MIN: "<<a[flag2].name<<" "<<a[flag2].scroe<<endl;

	return 0;
}