4.72 参考/判断三个数中的最大数

C++ 实例 - 判断三个数中的最大数

通过屏幕我们输入三个数字,并找出最大的数。

实例 - 使用 if

#include <iostream>
using namespace std;

int main()
{    
    float n1, n2, n3;

    cout << "请输入三个数: ";
    cin >> n1 >> n2 >> n3;

    if(n1 >= n2 && n1 >= n3)
    {
        cout << "最大数为: " << n1;
    }

    if(n2 >= n1 && n2 >= n3)
    {
        cout << "最大数为: " << n2;
    }

    if(n3 >= n1 && n3 >= n2) {
        cout << "最大数为: " << n3;
    }

    return 0;
}

以上程序执行输出结果为:

请输入三个数: 2.3
8.3
-4.2
最大数为: 8.3

实例 - 使用 if...else

#include <iostream>
using namespace std;

int main()
{
    float n1, n2, n3;

    cout << "请输入三个数: ";
    cin >> n1 >> n2 >> n3;

    if((n1 >= n2) && (n1 >= n3))
        cout << "最大数为: " << n1;
    else if ((n2 >= n1) && (n2 >= n3))
        cout << "最大数为: " << n2;
    else
        cout << "最大数为: " << n3;

    return 0;
}

以上程序执行输出结果为:

请输入三个数,以空格分隔: 2.3
8.3
-4.2
最大数为: 8.3

实例 - 使用内嵌的 if...else

#include <iostream>
using namespace std;

int main()
{
    float n1, n2, n3;

    cout << "请输入三个数: ";
    cin >> n1 >> n2 >> n3;

    if (n1 >= n2)
    {
        if (n1 >= n3)
            cout << "最大数为: " << n1;
        else
            cout << "最大数为: " << n3;
    }
    else
    {
        if (n2 >= n3)
            cout << "最大数为: " << n2;
        else
            cout << "最大数为: " << n3;
    }

    return 0;
}

以上程序执行输出结果为:

请输入三个数,以空格分隔: 2.3
8.3
-4.2
最大数为: 8.3
全部评论

相关推荐

找工作勤劳小蜜蜂:矛盾是没有实习,就是没实战经验,公司不想要,公司不要,你就没有实习,你就进入死循环,另外你的项目不是社会现在有大量岗位存在行业用的,云存储人员早就饱和。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务