fork download
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <vector>
  4. using std::cout;
  5. using std::endl;
  6. using std::vector;
  7.  
  8.  
  9. class Entity
  10. {
  11. public:
  12. Entity() : X(0), Y(0) {}
  13. Entity(int X, int Y) : X(X), Y(Y) {}
  14.  
  15. int X, Y;
  16. };
  17.  
  18.  
  19. struct {
  20. Entity PlayerLocation;
  21. vector<Entity> Entities;
  22. } fb;
  23.  
  24.  
  25. double distanceSquare(const Entity & p, const Entity & q) {
  26. double delta_x = p.X - q.X;
  27. double delta_y = p.Y - q.Y;
  28. return (delta_x * delta_x + delta_y * delta_y);
  29. }
  30.  
  31. Entity & GetNearestEntity()
  32. {
  33. return *std::min_element(fb.Entities.begin(), fb.Entities.end(),
  34. [&fb](const Entity & a, const Entity & b) {
  35. return distanceSquare(a, fb.PlayerLocation) <
  36. distanceSquare(b, fb.PlayerLocation);
  37. }
  38. );
  39. }
  40.  
  41. int main()
  42. {
  43. fb.Entities.push_back(Entity(1, 3));
  44. fb.Entities.push_back(Entity(5, 1));
  45. fb.Entities.push_back(Entity(-4, 2));
  46.  
  47. fb.PlayerLocation = Entity(6, 1);
  48.  
  49. Entity & nearest = GetNearestEntity();
  50. cout << nearest.X << "/" << nearest.Y << endl;
  51. }
  52.  
Success #stdin #stdout 0s 3016KB
stdin
Standard input is empty
stdout
5/1