I've just coded this for my country's programming Olympiad. I want to know if this method is a good approach in terms of readability and performance. I would also like to know how to improve it.
Note: We can assume all numbers entered are different, positive and smaller or equal to 100.
#include <stdio.h>
int find_second(int a, int b, int c)
{
if (a > b && b > c)
{
return b;
}
else if (a > c && c > b)
{
return c;
}
else
{
return -1;
}
}
int main(void)
{
int a, b, c, second1, second2, second3;
scanf("%d %d %d", &a, &b, &c);
second1 = find_second(a, b, c);
second2 = find_second(b, c, a);
second3 = find_second(c, a, b);
if (second1 != -1)
{
printf("%d\n", second1);
}
else if (second2 != -1)
{
printf("%d\n", second2);
}
else if (second3 != -1)
{
printf("%d\n", second3);
}
return 0;
}
1,1,1
or1,2,2
? You return-1
that's obviously, but what I'd expect is1
for the first case and1
for the second one. \$\endgroup\$ – Grajdeanu Alex Jun 24 '16 at 21:28<
operations) performed by your current code, on average and in the worst case. Can you do it in fewer? \$\endgroup\$ – Quuxplusone Jun 24 '16 at 21:49