码迷,mamicode.com
首页 > 编程语言 > 详细

第十五周oj刷题——Problem E: C++习题 对象数组求最大值

时间:2015-06-21 14:35:26      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:c++   iostream   对象   指针   namespace   

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

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

Output
成绩最高者的学号和成绩

Sample Input
5
101 78.5
102 85.5
103 98.5
104 100.0
105 95.5
Sample Output
104 100.00

/* All rights reserved. 
 * 文件名称:test.cpp 
 * 作者:陈丹妮 
 * 完成日期:2015年 6 月 21 日 
 * 版 本 号:v1.0 
 */  
#include <iostream>
#include <iomanip>
using namespace std;
class Student
{
private:
    int num;
    double s;
public:
    void input();
    void display();
    double get_s()
    {
        return s;
    }
    int get_num()
    {
        return num;
    }
};
void Student::input()
{
    cin>>num>>s;
}
void Student::display()
{
    cout<<num<<" "<<s<<endl;
}
void max(Student *p ,int n)
{
    int i,d=-1;
    double max=0;
    for(i=0; i<n; i++)
    {
        if((p+i)->get_s()>max)
            {
             max=(p+i)->get_s();
             d++;
            }
    }
    cout<<(p+d)->get_num()<<" "<<max;
}
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;
}

技术分享

学习心得:刷题,刷题,继续努力啦!!!相信自己!!

第十五周oj刷题——Problem E: C++习题 对象数组求最大值

标签:c++   iostream   对象   指针   namespace   

原文地址:http://blog.csdn.net/nufangdongde/article/details/46581235

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!