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

前端 未结 15 2154
遇见更好的自我
遇见更好的自我 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 07:53

    #include 
    #include 
    
    template 
    inline const T&
    max_of(const T& a, const T& b) {
        return std::max(a, b);
    }
    
    template 
    inline const T&
    max_of(const T& a, const T& b, const Args& ...args) {
        return max_of(std::max(a, b), args...);
    }
    
    int main() {
        std::cout << max_of(1, 2, 3, 4, 5) << std::endl;
        // Or just use the std library:
        std::cout << std::max({1, 2, 3, 4, 5}) << std::endl;
        return 0;
    }
    

提交回复
热议问题