D2.2求10 个整数中最大值 AND 将三个数按从大到小输出

本文介绍了一个简单的C语言程序,该程序接收三个整数输入,并输出这三个数中的最大值、次大值及最小值。程序首先读取两个初始数值并进行大小比较以确定当前的最大值和次大值,接着接收第三个数值并通过比较更新最大值、次大值和最小值。
#include <stdio.h>
//#define ALL

int main()
{
    int n, i, max, next, tmp;

    scanf("%d%d", &max, &next);
    if (next > max)
    {
        tmp = next;
        next = max;
        max = tmp;
    }

#if 0
    for (i = 2; i < 10; i++)
    {
        scanf("%d", &n);
        if (max < n)
        {
            next = max;
            max = n;
        }
        else if (next < n)
        {
            next = n;
        }
    }

    printf("最大数是%d  ,次大数是%d\n", max, next);
#else
    scanf("%d", &tmp);
    if (max < tmp)
    {
        printf("最大是%d , 次大是%d, 最小是%d\n", tmp, max, next);
    }
    else if (next < tmp)
    {
        printf("最大是%d , 次大是%d, 最小是%d\n", max, tmp, next);
    }
    else
    {
        printf("最大是%d , 次大是%d, 最小是%d\n", max, next, tmp);
    }
#endif
    system("pause");
    return 0;
}

 

在编写三个最大值的程序时,C语言和C++语言有以下一些区别: ### 语法层面 - **输入输出方式**:C语言使用`scanf`和`printf`函进行输入输出操作,而C++使用`cin`和`cout`对象进行输入输出,它们属于C++标准库中的`iostream`库。 ```c // C语言代码 #include <stdio.h> int main() { int a, b, c, max; printf("请输入3个要比较的整数:\n"); scanf("%d%d%d", &a, &b, &c); max = (a > b)? ((a > c)? a : c) : ((b > c)? b : c); printf("3个整数中的最大值max=%d\n", max); return 0; } ``` ```cpp // C++代码 #include <iostream> using namespace std; int main() { int a, b, c, max; cout << "请输入3个要比较的整数:" << endl; cin >> a >> b >> c; max = (a > b)? ((a > c)? a : c) : ((b > c)? b : c); cout << "3个整数中的最大值max=" << max << endl; return 0; } ``` - **函重载**:C++支持函重载,这意味着可以定义多个同名但参列表不同的函。在三个最大值时,可以根据不同的据类型定义不同的函。而C语言不支持函重载。 ```cpp // C++函重载示例 #include <iostream> using namespace std; int max(int a, int b, int c) { return (a > b)? ((a > c)? a : c) : ((b > c)? b : c); } double max(double a, double b, double c) { return (a > b)? ((a > c)? a : c) : ((b > c)? b : c); } int main() { int intMax = max(1, 2, 3); double doubleMax = max(1.1, 2.2, 3.3); cout << "整数最大值: " << intMax << endl; cout << "浮点最大值: " << doubleMax << endl; return 0; } ``` ### 编程范式层面 - **面向对象特性**:C++是面向对象的编程语言,在最大值程序中,可以将最大值的功能封装在类中,通过对象来调用。而C语言是面向过程的编程语言,主要通过函来实现功能。 ```cpp // C++面向对象示例 #include <iostream> using namespace std; class MaxFinder { public: int findMax(int a, int b, int c) { return (a > b)? ((a > c)? a : c) : ((b > c)? b : c); } }; int main() { MaxFinder finder; int result = finder.findMax(1, 2, 3); cout << "最大值: " << result << endl; return 0; } ``` ### 标准库层面 - **模板**:C++提供了模板机制,可以编写通用的代码来处理不同的据类型。在最大值时,可以使用模板函来实现。而C语言没有模板机制。 ```cpp // C++模板示例 #include <iostream> using namespace std; template <typename T> T max(T a, T b, T c) { return (a > b)? ((a > c)? a : c) : ((b > c)? b : c); } int main() { int intMax = max(1, 2, 3); double doubleMax = max(1.1, 2.2, 3.3); cout << "整数最大值: " << intMax << endl; cout << "浮点最大值: " << doubleMax << endl; return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值