Program to find largest and smallest among 5 numbers without using array

前端 未结 15 2284
遇见更好的自我
遇见更好的自我 2021-01-06 07:25

Yesterday I went for an interview where I have been asked to create a program to find largest and smallest among 5 numbers without using array.

I know how to create

15条回答
  •  长情又很酷
    2021-01-06 08:04

    int findMin(int t1, int t2, int t3, int t4, int t5)
    {
        int min1, min2, min3;
    
        min1 = std::min(t1, t2);
        min2 = std::min(t3, t4);
        min3 = std::min(min1, min2);
        return std::min(min3, t5);
    }
    
    int findMax(int t1, int t2, int t3, int t4, int t5)
    {
        int max1, max2, max3;
    
        max1 = std::max(t1, t2);
        max2 = std::max(t3, t4);
        max3 = std::max(max1, max2);
        return std::max(max3, t5);
    }
    

    These functions are very messy but easy to follow and thus easy to remember and it only uses the simple min and max methods which work best for 2 values.

提交回复
热议问题