Advertisement
smatskevich

Seminar6

Oct 24th, 2022
994
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.30 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <vector>
  5.  
  6. int main1() {
  7.   int n = 34;
  8.   std::cin >> n;
  9.   std::cout << (std::cin.operator bool() ? "Success" : "Fail") << std::endl;
  10.   std::cout << n;
  11.   return 0;
  12. }
  13.  
  14. int main2() {
  15.   std::vector<int> v;
  16.   int x = 23;
  17.   while (std::cin >> x) v.push_back(x);
  18.   for (int x : v) std::cout << x << " ";
  19.   std::cout << std::endl;
  20.   return 0;
  21. }
  22.  
  23. int main3() {
  24.   int n = 34, m = 2;
  25.   auto result = scanf("%d %d", &n, &m);
  26.   std::cout << n << " " << m << std::endl;
  27.   std::cout << result << std::endl;
  28.   return 0;
  29. }
  30.  
  31. int main4() {
  32.   unsigned int a = 0;
  33.   int b = 0;
  34.   std::cin >> a >> b;
  35.   // UNSIGNED!
  36.   auto result = (int)(b - a);
  37.   std::cout << result << std::endl;
  38.   return 0;
  39. }
  40.  
  41. void BubbleSort(std::vector<int>& v) {
  42.   for (int i = 0; i < v.size(); ++i) {
  43.     bool swapped = false;
  44.     for (int j = 0; j < v.size() - i - 1; ++j) {
  45.       if (v[j + 1] < v[j]) {
  46.         std::swap(v[j], v[j + 1]);
  47.         swapped = true;
  48.       }
  49.     }
  50.     if (!swapped) break;
  51.   }
  52. }
  53.  
  54. int main5() {
  55.   std::vector<int> v;
  56.   int x = 0;
  57.   while (std::cin >> x) v.push_back(x);
  58.   BubbleSort(v);
  59.  
  60.   for (int x : v) std::cout << x << " ";
  61.   std::cout << std::endl;
  62.   return 0;
  63. }
  64.  
  65. typedef long long ll;
  66. typedef void (* TPrintFunction)(int);
  67.  
  68. //void PrintVector(const std::vector<int>& v, void (* printer_function)(int)) {
  69. void PrintVector(const std::vector<int>& v, TPrintFunction printer_function) {
  70.   for (int x : v) printer_function(x);
  71.   std::cout << std::endl;
  72. }
  73.  
  74. void SimplePrinter(int x) {
  75.   std::cout << x << " ";
  76. }
  77.  
  78. void BeautifulPrinter(int x) {
  79.   std::cout << "**" << x << "** ";
  80. }
  81.  
  82. int main6() {
  83.   std::vector<int> v;
  84.   int x = 0;
  85.   while (std::cin >> x) v.push_back(x);
  86.  
  87.   PrintVector(v, SimplePrinter);
  88.   PrintVector(v, BeautifulPrinter);
  89.   return 0;
  90. }
  91.  
  92. struct Point {
  93.   int x = 0;
  94.   int y = 0;
  95. //  Вариант 1.
  96. //  bool operator<(const Point& right) {
  97. //    return x * x + y * y < right.x * right.x + right.y * right.y;
  98. //  }
  99. };
  100.  
  101. // Вариант 1.
  102. bool operator<(const Point& left, const Point& right) {
  103.   return left.x * left.x + left.y * left.y < right.x * right.x + right.y * right.y;
  104. }
  105.  
  106. void BubbleSort(std::vector<Point>& v) {
  107.   for (int i = 0; i < v.size(); ++i) {
  108.     bool swapped = false;
  109.     for (int j = 0; j < v.size() - i - 1; ++j) {
  110.       if (v[j + 1] < v[j]) {
  111.         std::swap(v[j], v[j + 1]);
  112.         swapped = true;
  113.       }
  114.     }
  115.     if (!swapped) break;
  116.   }
  117. }
  118.  
  119. // Вариант 2. Функция сравнения.
  120. bool CompareByModule(const Point& left, const Point& right) {
  121.   return left.x * left.x + left.y * left.y < right.x * right.x + right.y * right.y;
  122. }
  123.  
  124. void BubbleSort(std::vector<Point>& v, bool (* comp_function)(const Point&, const Point&)) {
  125.   for (int i = 0; i < v.size(); ++i) {
  126.     bool swapped = false;
  127.     for (int j = 0; j < v.size() - i - 1; ++j) {
  128.       if (comp_function(v[j + 1], v[j])) {
  129.         std::swap(v[j], v[j + 1]);
  130.         swapped = true;
  131.       }
  132.     }
  133.     if (!swapped) break;
  134.   }
  135. }
  136.  
  137. template <typename Compare>
  138. void BubbleSort2(std::vector<Point>& v, Compare c) {
  139.   for (int i = 0; i < v.size(); ++i) {
  140.     bool swapped = false;
  141.     for (int j = 0; j < v.size() - i - 1; ++j) {
  142.       if (c(v[j + 1], v[j])) {
  143.         std::swap(v[j], v[j + 1]);
  144.         swapped = true;
  145.       }
  146.     }
  147.     if (!swapped) break;
  148.   }
  149. }
  150.  
  151. // Функтор.
  152. class ComparatorByDistance {
  153.  public:
  154.   explicit ComparatorByDistance(const Point& center_) : center(center_) {}
  155.   bool operator()(const Point& left, const Point& right) const {
  156.     return SqrDistToCenter(left) < SqrDistToCenter(right);
  157.   }
  158.  
  159.  private:
  160.   Point center;
  161.  
  162.   int SqrDistToCenter(const Point& p) const {
  163.     return (p.x - center.x) * (p.x - center.x) + (p.y - center.y) * (p.y - center.y);
  164.   }
  165. };
  166.  
  167. int main7() {
  168.   int n = 0;
  169.   std::cin >> n;
  170.   std::vector<Point> v(n);
  171.   for (int i = 0; i < n; ++i) {
  172.     std::cin >> v[i].x >> v[i].y;
  173.   }
  174.   // Вариант 2.
  175.   BubbleSort(v, CompareByModule);
  176.   // Вариант 2. Передаем лямбду.
  177.   BubbleSort(v, [](const Point& left, const Point& right) -> bool {
  178.     return left.x < right.x;
  179.   });
  180.   // Вариант 3. Передаем все, что имеет ()
  181.   BubbleSort2(v, CompareByModule);
  182.   Point center = {4, 5};
  183.   BubbleSort2(v, ComparatorByDistance(center));
  184.  
  185.   BubbleSort2(v, [&center](const Point& left, const Point& right) -> bool {
  186.     auto dist_left = (left.x - center.x) * (left.x - center.x) + (left.y - center.y) * (left.y - center.y);
  187.     auto dist_right = (right.x - center.x) * (right.x - center.x) + (right.y - center.y) * (right.y - center.y);
  188.     return dist_left < dist_right;
  189.   });
  190.   for (const Point& p : v) {
  191.     std::cout << p.x << " " << p.y << std::endl;
  192.   }
  193.   return 0;
  194. }
  195.  
  196. int main() {
  197.   int n = 0;
  198.   std::cin >> n;
  199.   std::vector<Point> v(n);
  200.   for (int i = 0; i < n; ++i) {
  201.     std::cin >> v[i].x >> v[i].y;
  202.   }
  203.  
  204.   std::sort(v.begin(), v.end());
  205.   std::sort(v.begin(), v.end(), ComparatorByDistance({4, 5}));
  206.   // foo
  207. //  std::sort(v.begin(), v.end(), [](Point& left, Point& right) {
  208. //    left.x = 0;
  209. //    right.x = 0;
  210. //    return left.y < right.y;
  211. //  });
  212.  
  213.   for (const Point& p : v) {
  214.     std::cout << p.x << " " << p.y << std::endl;
  215.   }
  216.   return 0;
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement