Advertisement
jaVer404

level12.lesson12.bonus03

May 14th, 2015
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.47 KB | None | 0 0
  1. package com.javarush.test.level12.lesson12.bonus03;
  2.  
  3. /* Задача по алгоритмам
  4. Написать метод, который возвращает минимальное число в массиве и его позицию (индекс).
  5. */
  6.  
  7. public class Solution
  8. {
  9.     public static void main(String[] args) throws Exception
  10.     {
  11.         int[] data = new int[]{1, 2, 3, 5, -2, -8, 0, 77, 5, 5};
  12.  
  13.         Pair<Integer, Integer> result = getMinimumAndIndex(data);
  14.  
  15.         System.out.println("Minimum is " + result.x);
  16.         System.out.println("Index of minimum element is " + result.y);
  17.     }
  18.  
  19.     public static Pair<Integer, Integer> getMinimumAndIndex(int[] array)
  20.     {
  21.         if (array == null || array.length == 0)
  22.         {
  23.             return new Pair<Integer, Integer>(null, null);
  24.         }
  25.  
  26.         //Напишите тут ваше решение
  27.         else {
  28.             int minimum = array[0];
  29.             int index = 0;
  30.  
  31.             for (int temp = 1; temp < array.length; temp++){
  32.                 if (array[temp-1]< minimum) {
  33.                     minimum = array [temp - 1];
  34.                     index = temp - 1;
  35.                 }
  36.             }
  37.             return new Pair<Integer, Integer>(minimum, index);
  38.         }
  39.     }
  40.  
  41.  
  42.     public static class Pair<X, Y>
  43.     {
  44.         public X x;
  45.         public Y y;
  46.  
  47.         public Pair(X x, Y y)
  48.         {
  49.             this.x = x;
  50.             this.y = y;
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement