Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Задача 8.
- Имеется N прямоугольных конвертов и N прямоугольных открыток различных размеров.
- Можно ли разложить все открытки по конвертам, чтобы в каждом конверте было по одной
- открытке.
- Замечание. Открытки нельзя складывать, сгибать и т.п., но можно помещать в конверт под
- углом. Например, открытка с размерами сторон 5:1 помещается в конверты с размерами
- 5:1, 6:3, 4.3:4.3, но не входит в конверты с размерами 4:1, 10:0.5, 4.2:4.2.
- */
- #include <iostream>
- #include <Windows.h>
- #include <math.h>
- #include "Pair.h"
- #include <vector>
- using namespace std;
- void sorting_max(vector <Pair>&);
- void sorting_min(vector <Pair>&);
- void input(vector <Pair>&, int);
- void output(vector <Pair>);
- bool compare(double, double, double, double);
- int main()
- {
- SetConsoleOutputCP(1251);
- SetConsoleCP(1251);
- int q;
- do
- {
- int N;
- cout << "Введите количество открыток и конвертов:" << endl;
- cin >> N;
- vector <Pair> envelopes(N);
- vector <Pair> postcards(N);
- cout << "Введите размеры конвертов:" << endl;
- input(envelopes, N);
- cout << "Введите размеры открыток:" << endl;
- input(postcards, N);
- sorting_min(envelopes);
- sorting_max(postcards);
- cout << "Конверты:" << endl;
- output(envelopes);
- cout << "Открытки:" << endl;
- output(postcards);
- Pair tmp;
- bool can = false;
- cout << "Можно ли разложить все открытки по конвертам?" << endl;
- for (unsigned int i = 0; i < postcards.size(); i++)
- {
- can = false;
- for (unsigned int j = i; j < envelopes.size(); j++)
- {
- if (compare(envelopes[j].get_a(), envelopes[j].get_b(), postcards[i].get_a(), postcards[i].get_b()))
- {
- for (unsigned int k = j; k > 0; k--)
- {
- tmp = envelopes[k];
- envelopes[k] = envelopes[k - 1];
- envelopes[k - 1] = tmp;
- }
- can = true;
- continue;
- }
- }
- if (!can)
- {
- cout << "Нельзя. Для открытки " << postcards[i].get_a() << ':' << postcards[i].get_b() << " нет конверта" << endl;
- break;
- }
- }
- if (can)
- cout << "Можно" << endl;
- cin >> q;
- } while (q != 0);
- }
- bool compare(double e1, double e2, double p1, double p2)
- {
- if ((e1 >= p1) && (e2 >= p2))
- return true;
- else if (p1 * p1 + p2 * p2 > e1 * e1 + e2 * e2)
- return false;
- else if ((e1 * e2 > 2 * p1 * p2) &&
- ((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))
- return true;
- else return false;
- }
- void sorting_max(vector <Pair>& a)//сортирует вектор по убыванию пузырьком
- {
- Pair tmp;
- for (unsigned int i = 0; i < a.size() - 1; i++)
- {
- for (unsigned int j = 0; j < a.size() - i - 1; j++)
- {
- 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()))
- {
- tmp = a[j];
- a[j] = a[j + 1];
- a[j + 1] = tmp;
- }
- }
- }
- }
- void sorting_min(vector <Pair>& a)//сортирует вектор по возрастанию пузырьком
- {
- Pair tmp;
- for (unsigned int i = 0; i < a.size() - 1; i++)
- {
- for (unsigned int j = 0; j < a.size() - i - 1; j++)
- {
- 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()))
- {
- tmp = a[j];
- a[j] = a[j + 1];
- a[j + 1] = tmp;
- }
- }
- }
- }
- void input(vector <Pair>& a, int N)
- {
- Pair n3;
- double n1, n2;
- for (int i = 0; i < N; i++)
- {
- cin >> n1;
- cin >> n2;
- cout << endl;
- n3.initial(n1, n2);
- a[i] = n3;
- }
- }
- void output(vector <Pair> a)
- {
- for (unsigned int i = 0; i < a.size(); i++)
- {
- cout << a[i].get_a() << ':' << a[i].get_b() << endl;
- }
- }
- //файл Pair.h
- #pragma once
- #include <iostream>
- class Pair
- {
- double a, b;
- public:
- Pair();
- Pair(double, double);
- void set_a(double);
- void set_b(double);
- void initial(double, double);
- double get_a();
- double get_b();
- void output();
- friend std::ostream& operator<<(std::ostream& out, Pair& pair)
- {
- out << pair.get_a() << ':' << pair.get_b();
- return out;
- }
- };
- //файл Pair.cpp
- #include "Pair.h"
- Pair::Pair()
- {
- a = b = 0;
- }
- Pair::Pair(double a, double b)
- {
- if (a >= b)
- {
- this->a = a;
- this->b = b;
- }
- else
- {
- this->a = b;
- this->b = a;
- }
- }
- void Pair::set_a(double a)
- {
- this->a = a;
- }
- void Pair::set_b(double b)
- {
- this->b = b;
- }
- void Pair::initial(double a, double b)
- {
- if (a >= b)
- {
- this->a = a;
- this->b = b;
- }
- else
- {
- this->a = b;
- this->b = a;
- }
- }
- double Pair::get_a()
- {
- return a;
- }
- double Pair::get_b()
- {
- return b;
- }
- void Pair::output()
- {
- std::cout << a << ':' << b << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement