題目
使用重載函數模板重新可分別求取兩個整數,三個整數,兩個雙精度數,三個雙精度數的最大值
AC的C++代碼如下:
#include<iostream>
#include<algorithm>
using namespace std;
template <class T>
T Max1(T a,T b){
return max(a,b);
}
template <class T>
T Max1(T a,T b,T c){
return max(max(a,b),c);
}
int main()
{
int x,y,z;
//兩個整數
cout<<"Please enter two integer:\n";
cin>>x>>y;
cout<<Max1(x,y)<<endl;
//三個整數
cout<<"Please enter three integer:\n";
cin>>x>>y>>z;
cout<<Max1(x,y,z)<<endl;
double a,b,c;
//兩個雙精度數
cout<<"Please enter two double precision numbers:\n";
cin>>a>>b;
cout<<Max1(a,b)<<endl;
//三個雙精度數
cout<<"Please enter three double precision numbers:\n";
cin>>a>>b>>c;
cout<<Max1(a,b,c)<<endl;
return 0;
}