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

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

    Here's my implementation: Simple and short

    #include <iostream>
    #include <cstdio>
    using namespace std;
    
    
    int max_of_five(int a, int b, int c, int d,int e){
        int large= max(max(a, b), max(c,d));
        return max(large,e);
    
    }
    
    int min_of_five(int a,int b,int c, int d,int e){
        int small=min(min(a,b),min(c,d));
        return min(small,e);
    }
    
    
    int main() {
        int a, b, c, d,e;
    
        scanf("%d %d %d %d %d", &a, &b, &c, &d,&e);
        int ans = max_of_five(a, b, c, d, e);
        int ans1=min_of_five(a,b,c,d,e);
        printf("Max:\t%d\n", ans);
        printf("Min:\t%d", ans1);
    
        return 0;
    }
    
    0 讨论(0)
  • 2021-01-06 07:44

    Let max will hold the maximum of 5 numbers. Assign the first number to max. Take the 2nd number and compare it with max if the the 2nd number is greater than max then assign it to max else do nothing. Next take the 3rd number and compare it with max , if the 3rd number is greater than max assign it to max else do nothing. Do the same for 4th and 5th number. Finally max will hold the maximum of 5 number.

    0 讨论(0)
  • 2021-01-06 07:46

    Use a sorting network!

    #include <iostream>
    #include <utility>
    
    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';
    }
    
    0 讨论(0)
  • 2021-01-06 07:47

    If you like to keep things simple, then here is my solution.

    It works for any number of integers taken from standard input. It also works for negative integers. Enter end when you are done.

    #include <iostream>
    
    int main()
    {
    int max,min,input;
    std::cout<<"Enter the number: ";
    std::cin>>input;
    min=max=input;
    
    while(std::cin>>input){
        if(input>max) max=input;
        if(input<min) min=input;
        std::cout<<"Enter the number: ";
    }
    std::cout<<"\nMax: "<<max<<"\nMin: "<<min;
    }
    
    0 讨论(0)
  • 2021-01-06 07:49

    You can do something like this:

    int min_num = INT_MAX;  //  2^31-1
    int max_num = INT_MIN;  // -2^31
    int input;
    while (!std::cin.eof()) {
        std::cin >> input;
        min_num = min(input, min_num);
        max_num = max(input, max_num);
    }
    cout << "min: " << min_num; 
    cout << "max: " << max_num;
    

    This reads numbers from standard input until eof (it does not care how many you have - 5 or 1,000,000).

    0 讨论(0)
  • 2021-01-06 07:53
    #include <algorithm>
    #include <iostream>
    
    template <typename T>
    inline const T&
    max_of(const T& a, const T& b) {
        return std::max(a, b);
    }
    
    template <typename T, typename ...Args>
    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;
    }
    
    0 讨论(0)
提交回复
热议问题