Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************************
- Welcome to GDB Online.
- GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby,
- C#, VB, Perl, Swift, Prolog, Javascript, Pascal, HTML, CSS, JS
- Code, Compile, Run and Debug online from anywhere in world.
- *******************************************************************************/
- #include <iostream>
- #include <set>
- #include <cstdlib>
- #include <ctime>
- #include <algorithm>
- #include <vector>
- using namespace std;
- int main()
- {
- srand(time(0));
- //Способ задания универсального множества U: N случайных чисел из диапазона [a, b];
- int N = 20, a = -100, b = 100;
- set<int> s;
- for (int i = 0; i < N; i++){
- int x = rand() % (b - a + 1) + a;
- if (s.find(x) != s.end()){
- i--;
- continue;
- }
- s.insert(x);
- }
- cout << "U:\n";
- for (auto &x : s)
- cout << x << " | ";
- cout << "\n\n";
- //Способ выбора элементов множества A из универсального множества: Случайным образом (случайным образом выбираются индексы универсального множества)
- int N1 = 15;
- set<int> s1;
- for (int i = 0; i < N1; i++){
- auto it = s.begin();
- advance(it, rand() % N);
- int x = *(it);
- if (s1.find(x) != s1.end()){
- i--;
- continue;
- }
- s1.insert(x);
- }
- cout << "A:\n";
- for (auto &x : s1)
- cout << x << " | ";
- cout << "\n\n";
- //Способы выбора элементов множества B из универсального множества: Элементы, принадлежащие диапазону [c, d]
- int c = -50, d = 50;
- set<int> s2;
- for (auto it = s.lower_bound(c); it != s.upper_bound(d); it++)
- s2.insert(*it);
- cout << "B:\n";
- for (auto &x : s2)
- cout << x << " | ";
- cout << "\n\n";
- //Реализовать два преобразования над множествами A и B и показать идентичность их результата: (A or B и /(/A and /B))
- vector<int> or_vec(N);
- auto it = set_union(s1.begin(), s1.end(), s2.begin(), s2.end(), or_vec.begin());
- or_vec.resize(it - or_vec.begin());
- cout << "A or B:\n";
- for (auto &x : or_vec)
- cout << x << " | ";
- cout << "\n\n";
- vector<int> not_s1(N), not_s2(N), and_vec(N), not_and_vec(N);
- it = set_difference(s.begin(), s.end(), s1.begin(), s1.end(), not_s1.begin());
- not_s1.resize(it - not_s1.begin());
- it = set_difference(s.begin(), s.end(), s2.begin(), s2.end(), not_s2.begin());
- not_s2.resize(it - not_s2.begin());
- it = set_intersection(not_s1.begin(), not_s1.end(), not_s2.begin(), not_s2.end(), and_vec.begin());
- and_vec.resize(it - and_vec.begin());
- it = set_difference(s.begin(), s.end(), and_vec.begin(), and_vec.end(), not_and_vec.begin());
- not_and_vec.resize(it - not_and_vec.begin());
- cout << "/(/A and /B):\n";
- for (auto &x : not_and_vec)
- cout << x << " | ";
- cout << "\n\n";
- //Вывод результата: Нечетные по значению элементы
- cout << "Вывод результата:\n";
- for (auto &x : not_and_vec)
- if (x%2) cout << x << " | ";
- cout << "\n\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement