C++判断四个数的大小

1.   

#include 
using namespace std;

int findMax(int a, int b, int c, int d) 
{
    int maxNum = a;
    if (b > maxNum) 
    {
        maxNum = b;
    }
    if (c > maxNum) 
    {
        maxNum = c;
    }
    if (d > maxNum) 
    {
        maxNum = d;
    }
    return maxNum;
}

int main() 
{
    int num1, num2, num3, num4;
    cout << "请输入四个整数:" << endl;
    cin >> num1 >> num2 >> num3 >> num4;

    int maxNum = findMax(num1, num2, num3, num4);

    cout << "最大的数是:" << maxNum << endl;

    return 0;
}

2.

#include 
using namespace std;

int findMax(int a, int b, int c, int d) 
{
    int maxNum = (a > b) ? a : b;
    maxNum = (maxNum > c) ? maxNum : c;
    maxNum = (maxNum > d) ? maxNum : d;
    return maxNum;
}

int main() 
{
    int num1, num2, num3, num4;
    cout << "请输入四个整数:" << endl;
    cin >> num1 >> num2 >> num3 >> num4;

    int maxNum = findMax(num1, num2, num3, num4);

    cout << "最大的数是:" << maxNum << endl;

    return 0;
}

3.

#include 
using namespace std;

int findMax(int arr[], int size) 
{
    int maxNum = arr[0];
    for (int i = 1; i < size; i++) 
    {
        if (arr[i] > maxNum) 
        {
            maxNum = arr[i];
        }
    }
    return maxNum;
}

int main() 
{
    int nums[4];
    cout << "请输入四个整数:" << endl;
    for (int i = 0; i < 4; i++) 
    {
        cin >> nums[i];
    }

    int maxNum = findMax(nums, 4);

    cout << "最大的数是:" << maxNum << endl;

    return 0;
}

你可能感兴趣的:(C++,c++)