Advertisement
Guest User

jkjkjb

a guest
Feb 25th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.14 KB | None | 0 0
  1. package tp1afirst;
  2.  
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6.  
  7. /**
  8.  *
  9.  * @author joaom
  10.  */
  11. public class TP1Afirst {
  12.    
  13.     //Para calcular a distancia entre dois pontos
  14.     public static double distancia (int x1, int x2, int y1, int y2){
  15.         double coordx, coordy;
  16.        
  17.         coordx = Math.pow((x2 - x1),2);
  18.         coordy = Math.pow((y2 - y1),2);
  19.        
  20.         return Math.sqrt((coordx + coordy));
  21.     }
  22.    
  23.     //Para facilitar a leitura do input linha a linha
  24.     static String readLn (int maxLg){ //utility function to read from stdin
  25.         {
  26.         byte lin[] = new byte [maxLg];
  27.         int lg = 0, car = -1;
  28.         String line = "";
  29.         try {
  30.         while (lg < maxLg){
  31.         car = System.in.read();
  32.         if ((car < 0) || (car == '\n')) break;
  33.         lin [lg++] += car;
  34.         }
  35.         }
  36.         catch (IOException e){
  37.         return (null);
  38.         }
  39.         if ((car < 0) && (lg == 0)) return (null); // eof
  40.         return (new String (lin, 0, lg));
  41.         }
  42.     }
  43.    
  44.  
  45.     public static void main(String[] args) {
  46.  
  47.         String input = readLn(10); //Numero de pontos a receber
  48.         int num = Integer.parseInt(input);
  49.        
  50.         int aux; //Contar numero de linhas
  51.         int n_linhas = (int)(Math.ceil(num/10.0)); //Pormenor do 10.0
  52.        
  53.         //Duplicar o numero de pontos a receber (x,y)
  54.         String pontos;
  55.         String [] temp = new String [num];
  56.         ArrayList <String> array = new ArrayList <>();
  57.        
  58.         aux = 0;
  59.         while (aux < n_linhas){
  60.             pontos = readLn(100);
  61.             temp = pontos.split(" ");
  62.  
  63.             for (int x = 0; x < temp.length; x++)
  64.                 array.add(temp[x]);                  
  65.            
  66.             if (temp.length == 20 || temp.length == (num%10)*2){
  67.                 aux++;
  68.             }
  69.         }
  70.        
  71.         //Agupar pontos dois a dois (Array de arrays duplos)
  72.         int [][] total = new int [num][2];
  73.         int [] auxp = new int[2]; //Cada par de pontos        
  74.        
  75.         int j = 0;      
  76.         for (int i = 0; i < array.size(); i+=2){    
  77.             auxp[0] = Integer.parseInt(array.get(i));
  78.             auxp[1] = Integer.parseInt(array.get(i+1));
  79.            
  80.             total[j][0] = auxp[0];
  81.             total[j][1] = auxp[1];
  82.  
  83.             j++;
  84.         }
  85.        
  86.        
  87.        
  88.         //Percorrer, exaustivamente, todos os pares de pontos
  89.         int i;
  90.         double min;
  91.         min = distancia (total[0][0], total[1][0], total[1][0], total[1][1]);
  92.        
  93.         i = 0;      
  94.         while (i < num){
  95.            
  96.             for (int k = i + 1; k < num; k++){
  97.                 if (distancia (total[i][0], total[k][0], total[i][1], total[k][1]) < min){
  98.                     min = distancia (total[i][0], total[k][0], total[i][1], total[k][1]);
  99.                 }
  100.             }
  101.             i++;
  102.            
  103.         }
  104.        
  105.        
  106.         System.out.printf ("%f\n",min);
  107.        
  108.        
  109.        
  110.            
  111.            
  112.                        
  113.  
  114.            
  115.     }          
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement