fork download
  1. #include <iostream>
  2. #include <cmath>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. double dis(double x1, double y1, double x2, double y2){
  8. return (double) round(sqrt(pow((x1 - x2), 2) + pow((y1 - y2), 2))*100)/100;
  9. }
  10.  
  11. int main()
  12. {
  13. int s, curS;
  14. double cDis, eLoc, sDis;
  15. cin >> s;
  16. double locx [s];
  17. double locy [s];
  18. bool sheep [s];
  19. int ps [s];
  20.  
  21. std::cout << std::setprecision(2) << std::fixed;
  22.  
  23. for(int i = 0; i < s; i++){
  24. sheep[i] = false;
  25. ps[i] = 0;
  26. }
  27.  
  28. for(int i = 0; i < s; i++){
  29. cin >> locx[i];
  30. cin >> locy[i];
  31. }
  32. for(int i = 0; i < s; i++){
  33. eLoc = locx[i];
  34. cDis = locy[i];
  35. sDis = cDis;
  36. curS = 0;
  37. ps[curS] = i;
  38. curS++;
  39. for (int j = 0; j < s; j++){
  40. double nDis = dis(eLoc, 0, locx[j], locy[j]);
  41. if(nDis < sDis){
  42. sDis = nDis;
  43. curS = 0;
  44. ps[curS] = j;
  45. curS++;
  46. }
  47. else if(nDis == sDis){
  48. ps[curS] = j;
  49. curS++;
  50. }
  51. }
  52. for(int j = 0; j < curS; j++){
  53. sheep[ps[j]] = true;
  54. }
  55. }
  56. for(int i = 0; i < s; i++){
  57. if(sheep[i] == true){
  58. std::cout << "The sheep at (" << locx[i] << ", " << locy[i] << ") might be eaten." << endl;
  59. }
  60. }
  61. return 0;
  62. }
  63.  
Success #stdin #stdout 0s 4504KB
stdin
2
500
5
501
4
stdout
The sheep at (501.00, 4.00) might be eaten.