Problem J: C++类实现最大数的输出
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 349 Solved: 225
[ Submit][ Status][ Web Board]
Description
输入n个数,找出最大的数并输出。
Input
输入n,并输入n个数。
Output
输出的最大的数,每个输出结果占一行。
Sample Input
10 1 2 3 4 5 6 7 8 9 10
Sample Output
10
HINT
主函数未给出,需要自己加上。
class Q
{
public:
//此处声明需要的成员函数
private:
int n;
int a[100];
};
//下面定义成员函数
//用main()函数测试,完成输入输出
int main()
{
Q b1;
b1.set_value();
cout<<b1.get_Max()<<endl;
return 0;
}
#include <iostream>
using namespace std;
class Q
{
public:
void set_value();
int get_Max();
private:
int n;
int a[100];
};
void Q::set_value()
{
int i=0;
cin>>n;
while(cin>>a[i]&&i<n)
{
++i;
}
}
int Q::get_Max()
{
int m=0,i;
for(i=0;i<n-1;i++)
{
if(a[m]<a[i+1])
m=i+1;
}
return a[m];
}
int main()
{
Q b1;
b1.set_value();
cout<<b1.get_Max()<<endl;
return 0;
}