USACO BRHS10A Problem 'thirdfive' Analysis

by Damon Doucet

For this problem, a sorting function was required; you could either use a library version, or code one out yourself. Any variety of sorting functions would work.

Here's my solution:

#include <fstream>
using namespace std;

int array[5];

void swap(int i, int j) {
    int t = array[i];
    array[i] = array[j];
    array[j] = t;
}

void sort_array() {
    for (int i = 0; i < 5; i++)
        for (int j = i+1; j < 5; j++)
            if (array[i] < array[j])
                swap(i, j);
}

int main() {
    for (int i = 0; i < 5; i++)
        fin >> array[i];
    fout << array[2] << "\n"; //0-based indexing!
}

Below is a solution from Neal that does not require arrays:

#include <cstdio>
#include <algorithm>
using namespace std;

FILE *in = fopen ("thirdfive.in", "r"), *out = fopen ("thirdfive.out", "w");

int a, b, c, d, e;

void check (int &x, int &y)
{
    if (x > y)
        swap (x, y);
}

int main ()
{
    fscanf (in, "%d %d %d %d %d", &a, &b, &c, &d, &e);
    check (a, b);
    check (a, c);
    check (a, d);
    check (a, e);
    check (b, c);
    check (b, d);
    check (b, e);
    check (c, d);
    check (c, e);
    check (d, e);
    fprintf (out, "%d\n", c);
    return 0;
}