您的位置:首页 > 其它

面向对象程序设计上机练习二(函数模板)

2017-10-26 19:59 375 查看


面向对象程序设计上机练习二(函数模板)

Time Limit: 1000MS Memory Limit: 65536KB

Submit Statistic


Problem Description

利用数组和函数模板求5个数最大值(分别考虑整数、单精度、长整数的情况)。


Input

分别输入5个int型整数、5个float 型实数、5个long型正整数。


Output

分别输出5个int型整数的最大值、5个float 型实数的最大值、5个long型正整数的最大值。


Example Input

11 22 666 44 55
11.11 22.22 33.33 888.88 55.55
1234567 222222 333333 444444 555555



Example Output

666
888.88
1234567

代码如下:

#include<iostream>
using namespace std ;
template <typename T>
T max(T a[])
{
T m = 0 ;
int i ;
for(i=0;i<5;i++)
{
if(m<a[i])
{
m = a[i] ;
}
}
return m ;
}
int main()
{
int j;
int a[5];
float b[5];
long c[5];
for(j=0; j<5; j++)
cin >> a[j];
for(j=0; j<5; j++)
cin >> b[j];
for(j=0; j<5; j++)
cin >> c[j];
int e;
e=max(a);
cout << e << endl;
float f;
f=max(b);
cout << f << endl;
long g;
g=max(c);
cout << g << endl;
return 0 ;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: