kolioi

Near Cities (JA3-Task-2-Near-Cities) C++

Jan 14th, 2018
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.16 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <cfloat>
  5.  
  6. using namespace std;
  7.  
  8. typedef struct city
  9. {
  10.     string szCity;
  11.     float x;
  12.     float y;
  13.  
  14.     city(string _city, float _x, float _y) :
  15.         szCity(_city), x(_x), y(_y)
  16.     {}
  17.  
  18.     float distancef(const city& toCity) const
  19.     {
  20.         return (x - toCity.x)*(x - toCity.x) + (y - toCity.y)*(y - toCity.y);
  21.     }
  22. } City;
  23.  
  24. int main()
  25. {
  26.     cin.sync_with_stdio(false);
  27.     cout.sync_with_stdio(false);
  28.  
  29.     int nCount;
  30.     cin >> nCount;
  31.    
  32.     vector<City> cityList;
  33.     cityList.reserve(nCount);
  34.     for (int i = 0; i < nCount; i++)
  35.     {
  36.         string szCity;
  37.         int x, y;
  38.         cin >> szCity >> x >> y;
  39.         cityList.push_back(City(szCity, x, y));
  40.     }
  41.  
  42.     string closestCities[2];
  43.     float shortestDist = FLT_MAX;
  44.     for (vector<City>::iterator it1 = cityList.begin(); it1 != cityList.end(); it1++)
  45.     {
  46.         for (vector<City>::iterator it2 = it1+1; it2 != cityList.end(); it2++)
  47.         {
  48.             float d = it1->distancef(*it2);
  49.             if (d < shortestDist)
  50.             {
  51.                 shortestDist = d;
  52.                 closestCities[0] = it1->szCity;
  53.                 closestCities[1] = it2->szCity;
  54.             }
  55.         }
  56.     }
  57.    
  58.     cout << closestCities[0] << "-" << closestCities[1] << endl;
  59.  
  60.  
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment