Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <cmath>
- #include <iostream>
- #include <string>
- #include <vector>
- struct Point {
- int X;
- int Y;
- // Способ 1б. Оператор как метод внутри структуры.
- bool operator<(const Point& other) const {
- return X < other.X;
- }
- };
- // Способ 1. Оператор вне структуры/класса
- //bool operator<(const Point& p1, const Point& p2) {
- // return p1.X < p2.X;
- //}
- // Способ 2. Функция сравнения
- // Сравниваем по расстоянию от центра координат.
- bool CompareByDistance(const Point& p1, const Point& p2) {
- int dist1sqr = p1.X * p1.X + p1.Y * p1.Y;
- int dist2sqr = p2.X * p2.X + p2.Y * p2.Y;
- return dist1sqr < dist2sqr;
- }
- // Способ 3. Функтор.
- struct ComparatorByDistance {
- explicit ComparatorByDistance(const Point& center = {0, 0}) : Center(center) {}
- Point Center;
- bool operator()(const Point& p1, const Point& p2) const;
- };
- bool ComparatorByDistance::operator()(const Point& p1, const Point& p2) const {
- int dist1sqr = (p1.X - Center.X) * (p1.X - Center.X) + (p1.Y - Center.Y) * (p1.Y - Center.Y);
- int dist2sqr = (p2.X - Center.X) * (p2.X - Center.X) + (p2.Y - Center.Y) * (p2.Y - Center.Y);
- return dist1sqr < dist2sqr;
- }
- void sort(std::vector<Point>& data, const ComparatorByDistance& comparator) {
- int i = 0;
- for (; i < data.size(); i++) {
- bool swapped = false;
- for (int j = 0; j < data.size() - 1 - i; j++) {
- if (comparator(data[j + 1], data[j])) {
- std::swap(data[j], data[j + 1]);
- swapped = true;
- }
- }
- if (!swapped) break;
- }
- // std::cout << "iterations = " << i + 1 << std::endl;
- }
- //void sort(std::vector<Point>& data, bool (*comp)(const Point&, const Point&)) {
- // int i = 0;
- // for (; i < data.size(); i++) {
- // bool swapped = false;
- // for (int j = 0; j < data.size() - 1 - i; j++) {
- // if (comp(data[j + 1], data[j])) {
- // std::swap(data[j], data[j + 1]);
- // swapped = true;
- // }
- // }
- // if (!swapped) break;
- // }
- //// std::cout << "iterations = " << i + 1 << std::endl;
- //}
- // Считывание до fail.
- int main() {
- int n = 0;
- std::cin >> n;
- std::vector<Point> v(n);
- for(int i = 0; i < n; ++i) std::cin >> v[i].X >> v[i].Y;
- ComparatorByDistance comp;
- comp.Center = {3, 3};
- std::sort(v.begin(), v.end(), comp);
- //sort(v, comp);
- for (Point& x: v) {
- std::cout << x.X << " " << x.Y << std::endl;
- }
- //
- // int x = 0;
- // const int y = 0;
- // x = 4;
- // y = 4;
- return 0;
- }
- void SimplePrint(int x) {
- std::cout << x;
- }
- void BeautifulPrint(int x) {
- std::cout << "** " << x << " **" << std::endl;
- }
- typedef long long ll;
- typedef void (*printer_function)(int);
- void PrintVector(const std::vector<int>& x, void (*printer)(int)) {
- for (int value : x) {
- printer(value);
- }
- }
- void PrintVector2(const std::vector<int>& x, printer_function f) {
- for (int value : x) {
- f(value);
- }
- }
- int main1() {
- int n = 0;
- std::cin >> n;
- std::vector<int> v(n);
- for (int i = 0; i < n; ++i) std::cin >> v[i];
- PrintVector(v, &SimplePrint);
- PrintVector2(v, BeautifulPrint);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement