📌  相关文章
📜  程序找到第三个最小的数字 c++ 代码示例

📅  最后修改于: 2022-03-11 14:44:54.936000             🧑  作者: Mango

代码示例1
#include
#define MAX 100000
using namespace std;
 
int Print3Smallest(int array[], int n)
{
    int firstmin = MAX, secmin = MAX, thirdmin = MAX;
    for (int i = 0; i < n; i++)
    {
        /* Check if current element is less than
           firstmin, then update first, second and
           third */
        if (array[i] < firstmin)
        {
            thirdmin = secmin;
            secmin = firstmin;
            firstmin = array[i];
        }
 
        /* Check if current element is less than
        secmin then update second and third */
        else if (array[i] < secmin)
        {
            thirdmin = secmin;
            secmin = array[i];
        }
 
        /* Check if current element is less than
        then update third */
        else if (array[i] < thirdmin)
            thirdmin = array[i];
    }
 
    cout << "First min = " << firstmin << "\n";
    cout << "Second min = " << secmin << "\n";
    cout << "Third min = " << thirdmin << "\n";
}
 
// Driver code
int main()
{
    int array[] = {4, 9, 1, 32, 12};
    int n = sizeof(array) / sizeof(array[0]);
    Print3Smallest(array, n);
    return 0;
}