Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
3.3 kB
1
Indexable
Never
import java.util.*;

public class Main {
    public static int[] toIntArray(String str) {
        Scanner scanner = new Scanner(str);
        scanner.useLocale(Locale.ROOT);
        scanner.useDelimiter("(\\s|[,;])+");
        List<Integer> list = new ArrayList<>();
        while (scanner.hasNext()) {
            list.add(scanner.nextInt());
        }

        // из List<Integer> можно получить Integer[]
        Integer[] arr = list.toArray(new Integer[0]);
        // Integer[] -> int[]
        int[] result = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            // автоматическая распаковка из объекта
            result[i] = arr[i];
        }
        return result;
    }

    public static int[] readIntArrayFromConsole() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            try {
                System.out.println("Введите массив: ");
                String line = scanner.nextLine();
                return toIntArray(line);
            } catch (Exception e) {
                System.out.print("Вы ошиблись, попробуйте еще раз! ");
            }
        }
    }

    // %s - строка %d - целое число %n - новая строка
    public static void tests() {
        int[] input1 = new int[]{
                1, 6, 3, 7, 7, 2, -5, 8
        };
        System.out.printf("%s -> %d. %n", Arrays.toString(input1), solution(input1));
        int[] input2 = new int[]{
                1, 2, 3, 4, 5
        };
        System.out.printf("%s -> %d. %n", Arrays.toString(input2), solution(input2));
        int[] input3 = new int[]{
                5, 5, 5, 5, 5
        };
        System.out.printf("%s -> %d. %n", Arrays.toString(input3), solution(input3));
        int[] input4 = new int[]{
                -5, -5, -5, -5, -5
        };
        System.out.printf("%s -> %d. %n", Arrays.toString(input4), solution(input4));
        int[] input5 = new int[]{
                100, -5, 105, 1115, 125
        };
        System.out.printf("%s -> %d. %n", Arrays.toString(input5), solution(input5));
        int[] input6 = new int[]{
                -10, -5, -6, -8, 0
        };
        System.out.printf("%s -> %d. %n", Arrays.toString(input6), solution(input6));
        int[] input7 = new int[]{
                0, 0, 0, 0, 0
        };
        System.out.printf("%s -> %d. %n", Arrays.toString(input7), solution(input7));
    }

    public static int solution(int[] array) {
        int minimum = array[0];
        int secondMinimum = array[0];
        for (int current : array) {
            if (current < minimum) {
                secondMinimum = minimum;
                minimum = current;
            } else if (minimum == secondMinimum || (current < secondMinimum && current != minimum)) {
                secondMinimum = current;
            }
        }
        if (secondMinimum != minimum) {
            return secondMinimum;
        }
        return -1;
    }

    public static void main(String[] args) {
        tests();
        int[] input = readIntArrayFromConsole();
        System.out.println("Ответ: " + solution(input));

    }
}