用带有默认参数的函数求正整数中的最大数

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;
}

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632