C/C++

ПРОГРАМИРОВАНИЕ НА С++

Выбрать три точки заданного на плоскости множества точек, составляющие треугольник наибольшего периметра.
#include <iostream>
#include <cmath>
#include <set>
#include <vector>
using namespace std;
struct Point {
double x;
double y;
Point() : x(0), y(0) {}
Point(const double x, const double y) : x(x), y(y) {}
double length(const Point& p)const {
return sqrt(pow(p.x - x, 2) + pow(p.y - y, 2));
}
double raduis()const {
return sqrt(pow(x, 2) + pow(y, 2));
}
friend bool operator<(const Point& a, const Point& b) {
return a.raduis() < b.raduis();
}
friend ostream& operator<<(ostream& out, const Point& t) {
return out << "{ " << t.x << ", " << t.y << " }";
}
};
struct Triangle {
Point a;
Point b;
Point c;
Triangle() = default;
Triangle(const Point a, const Point b, const Point c) : a(a), b(b), c(c) {}
double area()const {
auto ab = a.length(b);
auto bc = b.length(c);
auto ca = c.length(a);
auto p = (ab + bc + ca) / 2;
return sqrt(p * (p - ab) * (p - bc) * (p - ca));
}
friend bool operator>(const Triangle& x, const Triangle& y) {
return x.area() > y.area();
}
friend ostream& operator<<(ostream& out, const Triangle& t) {
return out << "A: " << t.a << " B: " << t.b << " C: " << t.c;
}
};
Point set_coords() {
Point p;
cout << "x: ";
cin >> p.x;
cout << "y: ";
cin >> p.y;
puts("");
return p;
}
Triangle max_area(const set<Point>& points) {
vector<Point> box;
for (auto& point : points) box.push_back(point);
auto n = box.size();
Triangle max;
for (auto a = 0U; a < n; ++a) {
for (auto b = a + 1U; b < n; ++b) {
for (auto c = b + 1U; c < n; ++c) {
auto point = Triangle{ box[a], box[b], box[c] };
if (point > max) max = point;
}
}
}
return max;
}
int main() {
set<Point> points;
cout << "Quantity points: ";
size_t quantity;
cin >> quantity;
for (auto i = 0U; i < quantity; ++i) {
auto point = set_coords();
points.insert(point);
}
auto max = max_area(points);
cout << max << '\n';
system("pause > nul");
}
АК
Александр Конарыгин
52 540
Лучший ответ
С такой фамилией и не уметь программировать...
Руслан Назаров
Руслан Назаров
92 464
Пётр Писаренко хахахах чего
Перебирать все сочетания точек по 3 штуки. Для каждого сочетания считать сумму расстояний между всеми точками. И проверять, не лежат ли они все на одной прямой (чтобы треугольник вообще существовал), а также исключить выбор одной и той же точки несколько раз. Вывести ту тройку точек, для которых эта сумма будет максимальной.
Самый тупой, но простой для понимания и реализации способ.