首页 > 其他 > 详细

Problem A: 求个最大值

时间:2018-06-03 16:42:10      阅读:202      评论:0      收藏:0      [点我收藏+]
 

Problem A: 求个最大值

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 1635  Solved: 1339
[Submit][Status][Web Board]

Description

定义MaxValue类,用于求一系列非零整数的最大值。其中:

1. 数据成员elements用于存储所有输入的非零整数。

2. void append(int)用于向elements中添加一个新数据。

3. int getMax()用于求出elements中的最大值。

 

Input

输入若干个整数,以输入0表示输入结束。

 

Output

所有输入的非零整数中的最大值。

 

Sample Input

321
496
553
338
837
463
158
154
929
537
0

  

Sample Output

929

HINT

 

使用vector更为容易实现。


 

 

Append Code

append.cc
int main()
{
    int a;
    MaxValue test;
    cin>>a;
    while (a != 0)
    {
        test.append(a);
        cin>>a;
    }
    cout<<test.getMax()<<endl;
    return 0;
}

  

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class MaxValue
{
public :
    vector<int> elements;
    void append(int num)
    {
        elements.push_back(num);
    }
     int getMax()
     {
         sort(elements.begin(), elements.end());//排序
         return elements.back();//返回最后一个元素
     }
};
int main()
{
    int a;
    MaxValue test;
    cin>>a;
    while (a != 0)
    {
        test.append(a);
        cin>>a;
    }
    cout<<test.getMax()<<endl;
    return 0;
}

  

Problem A: 求个最大值

原文:https://www.cnblogs.com/Jie-Fei/p/9129484.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!