Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tp1afirst;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Arrays;
- /**
- *
- * @author joaom
- */
- public class TP1Afirst {
- //Para calcular a distancia entre dois pontos
- public static double distancia (int x1, int x2, int y1, int y2){
- double coordx, coordy;
- coordx = Math.pow((x2 - x1),2);
- coordy = Math.pow((y2 - y1),2);
- return Math.sqrt((coordx + coordy));
- }
- //Para facilitar a leitura do input linha a linha
- static String readLn (int maxLg){ //utility function to read from stdin
- {
- byte lin[] = new byte [maxLg];
- int lg = 0, car = -1;
- String line = "";
- try {
- while (lg < maxLg){
- car = System.in.read();
- if ((car < 0) || (car == '\n')) break;
- lin [lg++] += car;
- }
- }
- catch (IOException e){
- return (null);
- }
- if ((car < 0) && (lg == 0)) return (null); // eof
- return (new String (lin, 0, lg));
- }
- }
- public static void main(String[] args) {
- String input = readLn(10); //Numero de pontos a receber
- int num = Integer.parseInt(input);
- int aux; //Contar numero de linhas
- int n_linhas = (int)(Math.ceil(num/10.0)); //Pormenor do 10.0
- //Duplicar o numero de pontos a receber (x,y)
- String pontos;
- String [] temp = new String [num];
- ArrayList <String> array = new ArrayList <>();
- aux = 0;
- while (aux < n_linhas){
- pontos = readLn(100);
- temp = pontos.split(" ");
- for (int x = 0; x < temp.length; x++)
- array.add(temp[x]);
- if (temp.length == 20 || temp.length == (num%10)*2){
- aux++;
- }
- }
- //Agupar pontos dois a dois (Array de arrays duplos)
- int [][] total = new int [num][2];
- int [] auxp = new int[2]; //Cada par de pontos
- int j = 0;
- for (int i = 0; i < array.size(); i+=2){
- auxp[0] = Integer.parseInt(array.get(i));
- auxp[1] = Integer.parseInt(array.get(i+1));
- total[j][0] = auxp[0];
- total[j][1] = auxp[1];
- j++;
- }
- //Percorrer, exaustivamente, todos os pares de pontos
- int i;
- double min;
- min = distancia (total[0][0], total[1][0], total[1][0], total[1][1]);
- i = 0;
- while (i < num){
- for (int k = i + 1; k < num; k++){
- if (distancia (total[i][0], total[k][0], total[i][1], total[k][1]) < min){
- min = distancia (total[i][0], total[k][0], total[i][1], total[k][1]);
- }
- }
- i++;
- }
- System.out.printf ("%f\n",min);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement