Advertisement
Bibodui

Лаба 2 ЯМП (4 семестр)

Jun 1st, 2021
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.14 KB | None | 0 0
  1. /*Задача 8.
  2. Имеется N прямоугольных конвертов и N прямоугольных открыток различных размеров.
  3. Можно ли разложить все открытки по конвертам, чтобы в каждом конверте было по одной
  4. открытке.
  5. Замечание. Открытки нельзя складывать, сгибать и т.п., но можно помещать в конверт под
  6. углом. Например, открытка с размерами сторон 5:1 помещается в конверты с размерами
  7. 5:1, 6:3, 4.3:4.3, но не входит в конверты с размерами 4:1, 10:0.5, 4.2:4.2.
  8. */
  9.  
  10. #include <iostream>
  11. #include <Windows.h>
  12. #include <math.h>
  13. #include "Pair.h"
  14. #include <vector>
  15.  
  16. using namespace std;
  17.  
  18. void sorting_max(vector <Pair>&);
  19. void sorting_min(vector <Pair>&);
  20. void input(vector <Pair>&, int);
  21. void output(vector <Pair>);
  22. bool compare(double, double, double, double);
  23.  
  24. int main()
  25. {
  26.     SetConsoleOutputCP(1251);
  27.     SetConsoleCP(1251);
  28.  
  29.     int q;
  30.  
  31.     do
  32.     {
  33.         int N;
  34.         cout << "Введите количество открыток и конвертов:" << endl;
  35.         cin >> N;
  36.         vector <Pair> envelopes(N);
  37.         vector <Pair> postcards(N);
  38.         cout << "Введите размеры конвертов:" << endl;
  39.         input(envelopes, N);
  40.         cout << "Введите размеры открыток:" << endl;
  41.         input(postcards, N);
  42.         sorting_min(envelopes);
  43.         sorting_max(postcards);
  44.         cout << "Конверты:" << endl;
  45.         output(envelopes);
  46.         cout << "Открытки:" << endl;
  47.         output(postcards);
  48.         Pair tmp;
  49.         bool can = false;
  50.         cout << "Можно ли разложить все открытки по конвертам?" << endl;
  51.         for (unsigned int i = 0; i < postcards.size(); i++)
  52.         {
  53.             can = false;
  54.             for (unsigned int j = i; j < envelopes.size(); j++)
  55.             {
  56.                 if (compare(envelopes[j].get_a(), envelopes[j].get_b(), postcards[i].get_a(), postcards[i].get_b()))
  57.                 {
  58.                     for (unsigned int k = j; k > 0; k--)
  59.                     {
  60.                         tmp = envelopes[k];
  61.                         envelopes[k] = envelopes[k - 1];
  62.                         envelopes[k - 1] = tmp;
  63.                     }
  64.                     can = true;
  65.                     continue;
  66.                 }
  67.             }
  68.             if (!can)
  69.             {
  70.                 cout << "Нельзя. Для открытки " << postcards[i].get_a() << ':' << postcards[i].get_b() << " нет конверта" << endl;
  71.                 break;
  72.             }
  73.         }
  74.         if (can)
  75.             cout << "Можно" << endl;
  76.  
  77.         cin >> q;
  78.  
  79.     } while (q != 0);
  80. }
  81.  
  82.  
  83. bool compare(double e1, double e2, double p1, double p2)
  84. {
  85.     if ((e1 >= p1) && (e2 >= p2))
  86.         return true;
  87.     else if (p1 * p1 + p2 * p2 > e1 * e1 + e2 * e2)
  88.         return false;
  89.     else if ((e1 * e2 > 2 * p1 * p2) &&
  90.         ((p1 * p1 + p2 * p2 - e2 * e2) * (p1 * p1 + p2 * p2 - e1 * e1) <= e1 * e1 * e2 * e2 - 4 * e1 * e2 * p1 * p2 + 4 * p1 * p1 * p2 * p2))
  91.         return true;
  92.     else return false;
  93. }
  94.  
  95. void sorting_max(vector <Pair>& a)//сортирует вектор по убыванию пузырьком
  96. {
  97.     Pair tmp;
  98.     for (unsigned int i = 0; i < a.size() - 1; i++)
  99.     {
  100.         for (unsigned int j = 0; j < a.size() - i - 1; j++)
  101.         {
  102.             if ((a[j].get_a() < a[j + 1].get_a()) || (a[j].get_a() == a[j + 1].get_a()) && (a[j].get_b() < a[j + 1].get_b()))
  103.             {
  104.                 tmp = a[j];
  105.                 a[j] = a[j + 1];
  106.                 a[j + 1] = tmp;
  107.             }
  108.         }
  109.     }
  110. }
  111.  
  112. void sorting_min(vector <Pair>& a)//сортирует вектор по возрастанию пузырьком
  113. {
  114.     Pair tmp;
  115.     for (unsigned int i = 0; i < a.size() - 1; i++)
  116.     {
  117.         for (unsigned int j = 0; j < a.size() - i - 1; j++)
  118.         {
  119.             if ((a[j].get_a() > a[j + 1].get_a()) || (a[j].get_a() == a[j + 1].get_a()) && (a[j].get_b() > a[j + 1].get_b()))
  120.             {
  121.                 tmp = a[j];
  122.                 a[j] = a[j + 1];
  123.                 a[j + 1] = tmp;
  124.             }
  125.         }
  126.     }
  127. }
  128.  
  129.  
  130. void input(vector <Pair>& a, int N)
  131. {
  132.     Pair n3;
  133.     double n1, n2;
  134.     for (int i = 0; i < N; i++)
  135.     {
  136.         cin >> n1;
  137.         cin >> n2;
  138.         cout << endl;
  139.         n3.initial(n1, n2);
  140.         a[i] = n3;
  141.     }
  142. }
  143.  
  144. void output(vector <Pair> a)
  145. {
  146.     for (unsigned int i = 0; i < a.size(); i++)
  147.     {
  148.         cout << a[i].get_a() << ':' << a[i].get_b() << endl;
  149.     }
  150. }
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158. //файл Pair.h
  159. #pragma once
  160. #include <iostream>
  161. class Pair
  162. {
  163.     double a, b;
  164. public:
  165.     Pair();
  166.     Pair(double, double);
  167.     void set_a(double);
  168.     void set_b(double);
  169.     void initial(double, double);
  170.     double get_a();
  171.     double get_b();
  172.     void output();
  173.     friend std::ostream& operator<<(std::ostream& out, Pair& pair)
  174.     {
  175.         out << pair.get_a() << ':' << pair.get_b();
  176.  
  177.         return out;
  178.     }
  179. };
  180.  
  181.  
  182.  
  183.  
  184. //файл Pair.cpp
  185. #include "Pair.h"
  186.  
  187.     Pair::Pair()
  188.     {
  189.         a = b = 0;
  190.     }
  191.     Pair::Pair(double a, double b)
  192.     {
  193.         if (a >= b)
  194.         {
  195.             this->a = a;
  196.             this->b = b;
  197.         }
  198.         else
  199.         {
  200.             this->a = b;
  201.             this->b = a;
  202.         }
  203.     }
  204.     void Pair::set_a(double a)
  205.     {
  206.         this->a = a;
  207.     }
  208.     void Pair::set_b(double b)
  209.     {
  210.         this->b = b;
  211.     }
  212.     void Pair::initial(double a, double b)
  213.     {
  214.         if (a >= b)
  215.         {
  216.             this->a = a;
  217.             this->b = b;
  218.         }
  219.         else
  220.         {
  221.             this->a = b;
  222.             this->b = a;
  223.         }
  224.     }
  225.     double Pair::get_a()
  226.     {
  227.         return a;
  228.     }
  229.     double Pair::get_b()
  230.     {
  231.         return b;
  232.     }
  233.     void Pair::output()
  234.     {
  235.         std::cout << a << ':' << b << std::endl;
  236.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement