Advertisement
OverSkillers

Shortest Distance Exaustive

Feb 27th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.35 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package pontosrandom;
  7. import java.io.IOException;
  8. import java.util.*;
  9.  
  10. /**
  11.  *
  12.  * @author j_mig_000
  13.  */
  14. public class PontosRandom {
  15.  
  16.     /**
  17.      * @param args the command line arguments
  18.      */
  19.     static String readLn(int maxLg) { //utility function to read from stdin
  20.  
  21.         byte lin[] = new byte[maxLg];
  22.         int lg = 0, car = -1;
  23.         String line = "";
  24.         try {
  25.             while (lg < maxLg) {
  26.                 car = System.in.read();
  27.                 if ((car < 0) || (car == '\n')) {
  28.                     break;
  29.                 }
  30.                 lin[lg++] += car;
  31.             }
  32.         } catch (IOException e) {
  33.             return (null);
  34.         }
  35.         if ((car < 0) && (lg == 0)) {
  36.             return (null); // eof
  37.         }
  38.         return (new String(lin, 0, lg));
  39.     }
  40.    
  41.     static void distancia(int[] array){
  42.        
  43.         int x1,y1,x2,y2;
  44.         float temp,distancia = Integer.MAX_VALUE;
  45.        
  46.         for(int i = 0;i<array.length - 1; i += 2 ){
  47.             x1 = array[i];
  48.             y1 = array[i+1];
  49.             for (int j = 0; j < array.length - 1; j += 2) {
  50.                 x2 = array[j];
  51.                 y2 = array[j+1];
  52.                
  53.                 temp = (float) Math.sqrt((Math.pow(x2-x1,2) + Math.pow(y2 - y1,2)));
  54.                 if( j!= i && temp <= distancia){
  55.                     distancia = temp;
  56.                 }
  57.             }
  58.         }
  59.         System.out.printf("%.3f \n", distancia);
  60.     }
  61.  
  62.     /**
  63.      * @param args the command line arguments
  64.      */
  65.     public static void main(String[] args) {
  66.         // TODO code application logic here
  67.         int contador = 0;
  68.         String linha;
  69.         String tamanho = readLn(200);
  70.         String[] numeros;
  71.         int[] array = new int[Integer.parseInt(tamanho) * 2];
  72.        
  73.        
  74.         while (contador < (Integer.parseInt(tamanho))*2) {
  75.             linha = readLn(500);
  76.             numeros = linha.split(" ");
  77.             for(int i=0;i<numeros.length;i++){
  78.                 array[contador] = Integer.parseInt(numeros[i]);
  79.                 contador ++;
  80.             }
  81.         }
  82.        
  83.         distancia(array);        
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement