Created
November 23, 2017 03:32
-
-
Save P0huber/8e3bc1fa8f5021b398f66f20369da8e7 to your computer and use it in GitHub Desktop.
Finding Min digit and its index. Нахождение минимального числа массива и его индекс [Java]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MinAndIndex { | |
public static void main(String[] args) throws Exception { | |
int[] data = new int[]{1, 2, 3, 5, -2, -8, 0, 77, 5, 5}; | |
Pair<Integer, Integer> result = getMinimumAndIndex(data); | |
System.out.println("Minimum is " + result.x); | |
System.out.println("Index of minimum element is " + result.y);} | |
public static Pair<Integer, Integer> getMinimumAndIndex(int[] array) { | |
if (array == null || array.length == 0) { | |
return new Pair<Integer, Integer>(null, null);} | |
//напишите тут ваш код -- определение мин числа массива и его индекса | |
int min = array[0], x = 0; | |
for (int i = 1; i < array.length; i++) { | |
if (array[i] < min){ | |
min = array[i]; // сохраняем минимльное число массива | |
x = i;}} // переменнй х присваиваем индекс мин числа | |
return new Pair<Integer, Integer>(min, x);} | |
public static class Pair<X, Y> { | |
public X x; | |
public Y y; | |
public Pair(X x, Y y) { | |
this.x = x; | |
this.y = y; | |
} | |
} | |
} | |
/*Изоморфы наступают | |
Написать метод, который возвращает минимальное число в массиве и его позицию (индекс). | |
Требования: | |
1. Класс Solution должен содержать класс Pair. | |
2. Класс Solution должен содержать два метода. | |
3. Класс Solution должен содержать метод getMinimumAndIndex(). | |
4. Метод getMinimumAndIndex() должен возвращать минимальное число в массиве и его позицию (индекс).*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment