利用数组和函数模板求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
Author
#include <iostream>
using namespace std;
template <class T>
T max(T s[])
{
int i;
T max1=0;
for(i=1;i<=5;i++)
{
if(s[i]>max1)
{
max1=s[i];
}
}
return max1;
}
int main()
{
int a1[6];
float a2[6];
long a3[6];
int i;
for(i=1;i<=5;i++)
cin>>a1[i];
for(i=1;i<=5;i++)
cin>>a2[i];
for(i=1;i<=5;i++)
cin>>a3[i];
cout<<max(a1)<<endl;
cout<<max(a2)<<endl;
cout<<max(a3)<<endl;
return 0;
}