Untitled
unknown
c_cpp
2 months ago
1.4 kB
10
Indexable
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
using namespace std;
// Viet chuong trinh nhap vao n diem trong khong gian 2 chieu va mot diem x.
// Moi diem bao gom: ten diem, hoanh do, tung do.
// Tim diem gan nhat voi diem x.
// Vi du:
// Input:
// 3
// p1: 0 0
// p2: 1 8
// p3: 3 3
// x: 4 4
// Output: p3 (3, 3)
// !! Khoang cach giua hai diem a (x1, y1), b (x2, y2) duoc tinh theo cong thuc:
// sqrt((x1 - x2)^2 + (y1 - y2)^2)
struct point
{
string name;
int x, y;
double dist(point p)
{
return sqrt(pow(this->x - p.x, 2) + pow(this->y - p.y, 2));
}
};
int main()
{
int n, x, y;
vector<point> v;
string name;
point given_point;
cin >> n;
cin.ignore();
for (int i = 0; i < n; i++)
{
getline(cin, name, ':');
cin.ignore(1);
cin >> x >> y;
cin.ignore();
v.push_back({name, x, y});
}
getline(cin, name, ':');
cin.ignore(1);
cin >> x >> y;
given_point = {name, x, y};
double min_dist = given_point.dist(v[0]);
point nearest_point = v[0];
for (int i = 1; i < n; i++)
{
double dist = given_point.dist(v[i]);
if (dist < min_dist)
{
min_dist = dist;
nearest_point = v[i];
}
}
cout << nearest_point.name << " (" << nearest_point.x << ", " << nearest_point.y << ")";
return 0;
}
Editor is loading...
Leave a Comment