First Encounter with ACM — Luckily No Sorting Needed

Words: 93

The problem of finding the max and min of 5 numbers that I ran into back then

Seeing as I had no idea how to sort at the time

I realized this problem didn’t need sorting at all — just scan for them directly

What a relief!

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include<stdio.h>
int main()
{
int n,m[5],min,max;
for(n=0;n<5;n++)
scanf("%d",&m[n]);
max=min=m[1];
for(n=0;n<5;n++)
{   if(m[n]>=max)
    max=m[n];
    if(m[n]<=min)
    min=m[n];
}
printf("%d %d",min,max);
return 0;
}

Uh… though looking at it now… you know~

This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...