Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <windows.h>
- #include <ctime>
- #include <vector>
- #include <algorithm>
- #include <set>
- #include <map>
- #include <iomanip>
- #include <string>
- using namespace std;
- int main()
- {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- srand(time(NULL));
- system("color 0A");
- cout << "Введите через пробел длину массива a и b ";
- size_t sa, sb;
- cin >> sa >> sb;
- vector<int> a(sa);
- vector<int> b(sb);
- vector<int> x;
- auto filler = []()
- {
- auto value = rand() % 11;
- cout << setw(4u) << value;
- return value;
- };
- cout << "Содержание массива a" << endl;
- generate(a.begin(), a.end(), filler);
- cout << endl;
- cout << endl;
- cout << "Содержание массива b" << endl;
- generate(b.begin(), b.end(), filler);
- cout << endl;
- cout << endl;
- map<int, size_t> mp;
- set<int> st;
- for (const auto &value : b)
- {
- st.insert(value);
- }
- for (const auto &value : st)
- {
- mp.insert(make_pair(value, 0));
- }
- for (const auto &value : a)
- {
- ++mp[value];
- }
- for (const auto &value : b)
- {
- x.push_back(mp[value]);
- }
- cout << "Содержание массива x" << endl;
- for (const auto &value : x)
- {
- cout << setw(4u) << value;
- }
- cout << endl;
- cout << endl;
- vector<pair<int, size_t>> xx;
- for (const auto &value : st)
- {
- xx.push_back(make_pair(value, mp[value]));
- }
- auto predicate = [](
- pair<int, size_t> p1,
- pair<int, size_t> p2
- )
- {
- return p1.second > p2.second;
- };
- sort(xx.begin(), xx.end(), predicate);
- auto maxx = xx[0u].second;
- if (maxx == 0u)
- {
- cout << "Элементов массива B, входящих в массив A не обнаружено" << endl;
- }
- else
- {
- cout << "Элемент(ы) массива В чаще всего встречающийся(еся) в А" << endl;
- for (const auto &value : xx)
- {
- if (value.second == maxx)
- {
- cout << value.first << endl;
- }
- else
- {
- break;
- }
- }
- }
- cout << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement