6.15 喜刷刷 1 oj 1 对象数组中的最大值

来源:互联网 发布:c语言 十进制转换bcd 编辑:程序博客网 时间:2024/09/21 09:04
主页讨论版问题名次状态统计
系统测试进行中,遇到问题,请联系:18865513850 18865550239 18865513930 18865516568

问题 E: C++习题 对象数组求最大值

时间限制: 1 Sec  内存限制: 128 MB
提交: 1047  解决: 755
[提交][状态][讨论版]

题目描述

建立一个对象数组,内放n(<10)个学生的数据(学号、成绩),设立一个函数max,用指向对象的指针作函数参数,在max函数中找出n个学生中成绩最高者,并输出其学号。

输入

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

输出

成绩最高者的学号和成绩

样例输入

5101 78.5102 85.5103 98.5104 100.0105 95.5

样例输出

104 100.00
 

解题代码:

#include <iostream>#include <iomanip>using namespace std;class Student{public:    Student(int n=0,double s=0):num(n),score(s) {}    int get_num();    void input();    double get_score();    void display();

private:    int num;   //学号    double score;   //成绩};

int Student::get_num(){    return num;}double Student::get_score(){    return score;}void max(Student *arr,int n){    double max=arr[0].get_score();    int i,no;    for(i=0; i<n; ++i)    {        if(arr[i].get_score()>max)            no=i;    }    no--;    arr[no].display();}void Student::input(){    double n,s;    cin>>n>>s;    num=n;    score=s;}void Student::display(){    cout<<num<<" "<<score<<endl;}

int main()

{    void max(Student* ,int);    const int NUM=10;    Student stud[NUM];    int n,i;    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;}

0 0
原创粉丝点击