【OJ】---M---对象数组求最大值



题目要求如下:

-----------------------------------------------------------------------------------------------------------------------------------------------

代码如下:

运行结果:

/*
 * Copyright (c) 2013, 烟台大学计算机学院
 * All rights reserved.
 * 作    者:  沈远宏
 * 完成日期:2014 年06月18日
 * 版 本 号:v1.0
 * 问题描述:Description
建立一个对象数组,内放n(<10)个学生的数据(学号、成绩),设立一个函数max,用指向对象的指针作函数参数,在max函数中找出n个学生中成绩最高者,并输出其学号。

Input
n和n个学生的学号、成绩

Output
成绩最高者的学号和成绩
*/#include <iostream>
#include <iomanip>
#include <cstring>
#include <fstream>
using namespace std;
//主函数已给定如下,提交时不需要包含下述主函数
class Student
{
private:
    string no;
    double score;
public:
    void input()
    {
        cin>>no>>score;
    }
    void display()
    {
        cout<<no<<" "<<score<<endl;
    }
    double get_score()
    {
        return score;
    }
    string get_no()
    {
        return no;
    }
};
void max(Student* p,int n)
{
    int index=0;
    for(int j=0; j<n; ++j)
    {
        for(int i=0;i<n-j;++i)
        {
            if(p[i].get_score()>p[i+1].get_score())
            {
                index=i;
            }
        }
    }
    cout<<p[index].get_no()<<" "<<p[index].get_score()<<endl;
}
int main()
{
    void max(Student* p,int);
    const int NUM=10;
    Student stud[NUM];
    int n,i;
    freopen("13.txt","r",stdin);
    cin>>n;
    for(i=0; i<n; i++)
        stud[i].input();
    cout<<setiosflags(ios::fixed);
    cout<<setprecision(2);
    Student *p=&stud[0];
    max(p,n);
    return 0;
}




OJ要求结果输出例样:

发布了173 篇原创文章 · 获赞 8 · 访问量 11万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章