Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class LargestThree {
- public static void main(String... args) {
- Scanner in = new Scanner(System.in);
- int numbersCount = in.nextInt();
- int n1 = Integer.MIN_VALUE; // largest
- int n2 = Integer.MIN_VALUE; // second largest
- int n3 = Integer.MIN_VALUE; // third largest
- int x;
- for (int i = 0; i < numbersCount; ++i) {
- x = in.nextInt();
- if (n1 < x) { // is greater than the largest
- n3 = n2;
- n2 = n1;
- n1 = x;
- } else if (n2 < x) { // is not greater than the largest but is greater than the second largest
- n3 = n2;
- n2 = x;
- } else if (n3 < x) { // is not greater than the second largest but is greater than the third largest
- n3 = x;
- }
- // if we are here, then is not greater than the third largest so we do not care and just skip it
- }
- System.out.printf("%d, %d and %d", n1, n2, n3);
- }
- }
Add Comment
Please, Sign In to add comment