c++中求2个或者3个正整数中的最大数,用带有默认参数的函数实现
代码如下:
#include <iostream>
using namespace std;
double max(double a,double b,double c=0)
{
double t = a>(b>c?b:c)?a:(b>c?b:c);
return t;
}
int main()
{
double a,b,c;
cin>> a>>b>>c;
cout << max(a,b)<<endl;
cout << max(a,b,c)<<endl;
return 0;
}
或者:
#include <iostream>
using namespace std;
double max(double a,double b,double c=0)
{
double t;
if(a>b) t = a;
else t = b;
if(c>t) t=c;
return t;
}
int main()
{
double a,b,c;
cin>> a>>b>>c;
cout << max(a,b)<<endl;
cout << max(a,b,c)<<endl;
return 0;
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!