kxcoze

tanya_makoh_lab??

Apr 27th, 2021
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1. #include <iostream>
  2. #include <random>
  3. #include <time.h>
  4. #include <utility>
  5. #include <string>
  6. #include <vector>
  7. #include <algorithm>
  8.  
  9. using namespace std;
  10.  
  11. pair<int, pair<int, string>> convert_to_bin(int);
  12.  
  13. int main() {
  14.     srand(time(0));
  15.     setlocale(LC_ALL, "Rus");
  16.     int n, maxi = -1;
  17.     vector <pair<int, pair<int, string>>> vec;
  18.     cout << "Введите кол-во чисел: ";
  19.     cin >> n;
  20.     for (int i = 0; i < n; i++) {
  21.         int m;
  22.         cout << i + 1 << ") m = ";
  23.         m = rand() % 1000;
  24.         pair <int, pair<int, string>> converted_num = convert_to_bin(m);
  25.         vec.push_back(converted_num);
  26.  
  27.         cout << m << " --> " << converted_num.first << ' ' << converted_num.second.second << '\n';
  28.  
  29.         maxi = max(maxi, converted_num.first);
  30.     }
  31.     cout << endl << "Числа/о, в двоичном представлении которого больше всего единиц: " << maxi << '\n';
  32.     for (pair<int, pair<int, string>> p : vec) {
  33.         if (p.first == maxi)
  34.             cout << p.second.first << " --> " << p.second.second << '\n';
  35.     }
  36.     return 0;
  37. }
  38.  
  39. pair<int, pair<int, string>> convert_to_bin(int integer) {
  40.     string converted_s = "";
  41.     int counter = 0;
  42.     int copy_i = integer;
  43.     while (integer >= 1) {
  44.         if (integer % 2 == 1) counter++;
  45.         converted_s += (integer % 2) + '0';
  46.         integer = integer / 2;
  47.     }
  48.     reverse(converted_s.begin(), converted_s.end());
  49.     return make_pair(counter, make_pair(copy_i, converted_s));
  50. }
Add Comment
Please, Sign In to add comment