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

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

    Use a sorting network!

    #include 
    #include 
    
    int main()
    {
        int a, b, c, d, e;
        std::cin >> a >> b >> c >> d >> e;
    
        if (a < b) std::swap(a, b);
        if (d < e) std::swap(d, e);
        if (c < e) std::swap(c, e);
        if (c < d) std::swap(c, d);
        if (b < e) std::swap(b, e);
        if (a < d) std::swap(a, d);
        if (a < c) std::swap(a, c);
        if (b < d) std::swap(b, d);
        if (b < c) std::swap(b, c);
    
        std::cout << "largest = " << a << '\n';
        std::cout << "smallest = " << e << '\n';
    }
    

提交回复
热议问题