2. Where's the Biggest One?
by CodeChum Admin
We've already tried comparing 3 numbers to see the largest among all, so let's try a more complicated mission where we locate the position of the largest among 5 numbers. There are only 4 possible cases for this mission:
- if the largest digit is the first digit, print "Leftmost"
- if the largest digit is the third digit, print "Middle"
- if the largest digit is the last digit, print "Rightmost"
- if none of the above is correct, print "Unknown"
Now, show me how far you've understood your lessons!
Input
A line containing a five-digit integer.
1·4·6·3·2
Output
A line containing a string.
Middle
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner S = new Scanner(System.in);
String N[] = S.nextLine().trim().split(" ");
int number = Integer.parseInt(N[0]);
int max = number;
int maximumIndex = 0;
for (int j = 0; j < N.length; j++) {
number = Integer.parseInt(N[j]);
if (number > max) {
max=number;
maximumIndex = j;
}
}
if (maximumIndex <= 1) {
System.out.println("Leftmost");
}else
if (maximumIndex == 2) {
System.out.println("Middle");
}else
if (maximumIndex >2) {
System.out.println("Rightmost");
}else
{
System.out.println("Unknown");
}
S.close();
}
}
Comments
Leave a comment