Created
November 12, 2017 20:52
-
-
Save 13andrew13/cf15c4cde17faf3992e4af9a145845c5 to your computer and use it in GitHub Desktop.
Find Distance
Твоя задача создать консольное приложение, которое принимало бы на вход ряд чисел и выводило расстояние между двумя наименьшими. Например, дано ряд чисел: "23 45 34 12 45 4 38 56 2 49 100". Наименьшие числа в нем 2 и 4. Расстояние между ними - 3.
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
package com.company; | |
import java.util.Arrays; | |
import java.util.regex.Pattern; | |
import java.util.stream.IntStream; | |
public class Main { | |
public static void main(String[] args) { | |
int a[]= Pattern.compile(" ").splitAsStream(args[0]).mapToInt(Integer::parseInt).toArray(); | |
int n1= getIndex(a,0); | |
int n2= getIndex(a,1); | |
System.out.println(Math.abs(n1-n2)); | |
} | |
private static int getIndex(int[] a,int index) { | |
int x = getMin(a,index); | |
return IntStream.range(0,a.length).filter(i->x==a[i]).findFirst().orElse(-1); | |
} | |
public static int getMin(int[]a,int index){ | |
int arr[] =new int[a.length]; | |
System.arraycopy(a,0,arr,0,arr.length); | |
Arrays.sort(arr); | |
return arr[index]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment