Skip to content

Instantly share code, notes, and snippets.

@eribeiro
Created December 10, 2011 00:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eribeiro/1454028 to your computer and use it in GitHub Desktop.
Save eribeiro/1454028 to your computer and use it in GitHub Desktop.
import static java.util.Arrays.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class Pizza {
public static void main(String[] args) {
Map<String, List<Integer>> sabores = new HashMap<String, List<Integer>>();
sabores.put("Renato", asList(4, 5, 4, 5, 4, 3));
sabores.put("Marcelo", asList(2, 2, 1, 3, 5, 2));
sabores.put("Lenon", asList(4, 5, 2, 1, 1, 3));
sabores.put("Renata", asList(4, 5, 1, 1, 3, 4));
sabores.put("Washington", asList(1, 1, 2, 3, 4, 3));
sabores.put("Tino", asList(1, 5, 1, 4, 3, 2));
List<Integer> luca = asList(5, 4, 3, 4, 3, 2);
String pessoa = "";
Double distance = Double.MAX_VALUE;
for (Entry<String, List<Integer>> e : sabores.entrySet()) {
List<Integer> list = e.getValue();
// distância euclidiana
double d = 0;
for (int i = 0; i < list.size(); i++) {
d += Math.pow(list.get(i) - luca.get(i), 2);
}
double res = Math.sqrt(d);
// fim distância euclidiana
if (res < distance) {
pessoa = e.getKey();
distance = res;
}
}
System.out.println(pessoa + " " + distance);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment