How to find the second minimum number without using arrays or loops

I tried to write a program that receives 5integers from the user and prints the second minimum number.
Here is an example of what I tried:

#include <iostream>
using namespace std;
int main () {
int a,b,c,d,e;
cin>>a>>b>>c>>d>>e;
if (a>b && a<c && a<d && a<e)
    cout<<a<<endl;
if (b>a && b<c && b<d && b<e)
    cout<<b<<endl;
if (c>a && c<b && c<d && c<e)
    cout<<c<<endl;
if (d>a && d<b && d<c && d<e)
    cout <<d<<endl;
if (e>a && e<b && e<c && e<d)
cout <<e<<endl;
return 0;

}

When I enter 1 2 3 4 5, it prints the second minimum, but when I enter the 5 4 3 2 1screen nothing will be printed. What am I doing wrong with this? Is there any other way to write my program?

+4
source share
8 answers

, . "else" if/else, , . , , .

, , , (1), (4). , , , . b > a 5,4...

: - std::vector/std::, , - , .

-

vector<int> data;
for (int i=0; i<5; ++i) {
    int t;
    cin >> t;
    data.push_back(t);
}
std::nth_element(data.begin(), data.begin()+1, data.end(), std::greater<int>());
cout << data[1];
+4

120 5 . . , 120 , :

if (a > b && b > c && c > d && d > e) // the order is a>b>c>d>e
    cout << d;
else if (a > b && b > c && c > e && e > d) // the order is a>b>c>e>d
    cout << e;
...
else if (e > d && d > c && c > a && e > b) // the order is e>d>c>a>b
    cout << a;
else // the order is e>d>c>b>a
    cout << b;

, . , . , , .


, , . , , 5, , . 5 9 , . .

, , , 7 .

( 9 7) ++:

if (b < c)
    swap(b, c);
if (d < e)
    swap(d, e);
if (b < d)
    swap(b, d);
if (a < c)
    swap(a, c);
if (c < e)
    swap(c, e);
if (a < d)
    swap(a, d);
if (a < b)
    swap(a, b);
if (c < d)
    swap(c, d);
if (b < c)
    swap(b, c);
// now the order is a ≥ b ≥ c ≥ d ≥ e
cout << d;

- , , . , , - ( ) .

- , , , . , 120 ( 32, 1), , .

+3

. ( , , min secondMin , :

#include <iostream>
using namespace std;
int main () {
int a,b,c,d,e;
int min, secondMin;


cin>>a>>b;

min = a < b ? a : b;
secondMin = a < b ? b : a;

cin>>c;

if (c < min)
{
    secondMin = min;
    min = c;
}
else if (c < secondMin)
{
    secondMin = c;
}

cin>>d;

if (d < min)
{
    secondMin = min;
    min = d;
}
else if (c < secondMin)
{
    secondMin = d;
}

cin>>e;

if (e < min)
{
    secondMin = min;
    min = e;
}
else if (e < secondMin)
{
    secondMin = e;
}

cout << "min = " << min << ", secondMin = " << secondMin << endl;

return 0;

}

- ,

+2
#include <set>

std::set<int> values = { a, b, c, d, e }; // not an array.
int second_min = *std::next(values.begin(), 1); // not a loop
+2

? , 5 .

get_2nd_min() , std::cin, count :

#include <climits>
#include <cstddef>
#include <iostream>

int get_2nd_min(size_t count, int min = INT_MAX, int second_min = INT_MAX)
{
   if (!count)
      return second_min; // end of recursion

   // read next value from cin
   int value;
   std::cin >> value;

   // Does second_min need to be updated?
   if (value < second_min) {

     // Does min also need to be updated?
     if (value < min) {
        // new min found
        second_min = min; // move the so far min to second_min
        min = value; // update the new min
     } else {
        // value is lower than second_min but higher than min
        second_min = value; // new second_min found, update it
     }
   }
   // perform recursion
   return get_2nd_min(count - 1, min, second_min);
}

5 :

int second_min = get_2nd_min(5);
+1

min:

#include <iostream>
using namespace std;


int minDifferentFromFirstMin(int x, int y, int firstMin) {
    if(x < y) {
        if(x != firstMin) {
            return x;
        }
        else {
            return y;
         }
    }

    if(y < x) {
        if(y != firstMin) {
            return y;
        }
        else {
            return x;
         }
    }
    //if x & y are equals, return one of them
    return x;
}

int main () {
 int a,b,c,d,e;
 int iter11, iter12, iter13;
 int iter21, iter22, iter23;
 int firstMinimum, secondMinimum;
 cin>>a>>b>>c>>d>>e;

 //iteration 1: find the first minimum
 iter11 = min(a, b);
 iter12 = min(c, d);
 iter13 = min(iter11, iter12);
 firstMinimum = min(iter13, e);

 //iteration 2: find the second minimum
 iter21 = minDifferentFromFirstMin(a, b, firstMinimum);
 iter22 = minDifferentFromFirstMin(c, d, firstMinimum);
 iter23 = minDifferentFromFirstMin(iter21, iter22, firstMinimum);
 secondMinimum = minDifferentFromFirstMin(iter23, e, firstMinimum);

 cout<<secondMinimum<<endl;
}
0

, min, , min. :

int min = std::min(a, std::min(b, std::min(c, std::min(d, e))));

, min. , triMin, 3 , :

int triMin(int currentMin, int left, int right)
{
    if(currentMin == left) return right;
    if(currentMin == right) return left;

    return std::min(left, right);
}

, :

int a = 5, b = 4, c = 3, d = 2, e = 1;

int min = std::min(a, std::min(b, std::min(c, std::min(d, e))));
int min2 = triMin(min, a, triMin(min, b, triMin(min, c, triMin(min, d, e))));

std::cout << "Second min = " << min2 << std::endl;

2

0

one-pass. - (, - ).
- ( ) , - .
:

  • minimum second minimum .
  • , minumum, , .
    • minimum, second minimum
  • , second minimum.
    • second minimum.
  • second minimum .
  • , .

second minimum .
++ 17 ( wandbox):

#include <iostream>
#include <optional>

int main()
{
    int a, b, c, d, e;
    std::cin >> a >> b >> c >> d >> e;

    // you can find second minimal number while going through each number once
    auto find_current_answer = [minimum = std::optional<int>{}, next_to_minimum = std::optional<int>{}](int next) mutable {
        // when receiving next number
        // 1. check if it is new minimum
        if (!minimum || minimum > next) {
            // move values like this: next_to_minimum <- minimum <- next
            next_to_minimum = std::exchange(minimum, next);
        }
        // 2. else check if it is new next_to_minimum
        else if (!next_to_minimum || next_to_minimum > next) {
            next_to_minimum = next;
        }
        // 3. return current answer
        return next_to_minimum;
    };

    // repeat as much as you like
    find_current_answer(a);
    find_current_answer(b);
    find_current_answer(c);
    find_current_answer(d);
    // store answer that is interesting to you
    auto result = find_current_answer(e);

    // if it has value - it is the answer
    if (result) {
        std::cout << "Answer: " << *result << '\n';
    }
    else {
        std::cout << "Not enough numbers!\n";
    }
}
0

Source: https://habr.com/ru/post/1690275/


All Articles