Advertisement
vov44k

Поиск 2х максимумов

Feb 27th, 2022
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.75 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner in = new Scanner(System.in);
  7.  
  8.         int n = in.nextInt();
  9.         int[] a = new int[n];
  10.         for (int i = 0; i < n; i++) {
  11.             a[i] = in.nextInt();
  12.         }
  13.         int max1 = a[0], max2 = a[1];
  14.         for (int i = 0; i < n; i++) {
  15.             if (max1 < a[i]) {
  16.                 max2 = max1;
  17.                 max1 = a[i];
  18.             } else if (max2 < a[i]) {
  19.                 max2 = a[i];
  20.             }
  21.         }
  22.         int temp;
  23.         if (max2 > max1) {
  24.             temp = max2;
  25.             max2 = max1;
  26.             max1 = temp;
  27.         }
  28.         System.out.print(max1 + " " + max2);
  29.  
  30.         in.close();
  31.     }
  32. }
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement