Advertisement
iamweasel

Untitled

Dec 12th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <random>
  3. #include <algorithm>
  4. #include <functional>
  5. using namespace std;
  6.  
  7. const int n = 100;
  8.  
  9. int main() {
  10.     double arr1[n];
  11.     int arr2[n];
  12.     double a, b;
  13.    
  14.     std::random_device r;
  15.  
  16.     std::default_random_engine generator(r());
  17.    
  18.     cin >> a >> b;
  19.    
  20.     std::uniform_real_distribution<double> distribution1(a, b);
  21.     auto dice_roll_1 = std::bind(distribution1, generator);
  22.  
  23.     std::uniform_int_distribution<int> distribution2(trunc(a), trunc(b));
  24.     auto dice_roll_2 = std::bind(distribution2, generator);
  25.    
  26.     std::generate_n(std::begin(arr1), n, dice_roll_1);
  27.     std::generate_n(std::begin(arr2), n, dice_roll_2);
  28.    
  29.     std::for_each(std::begin(arr1), std::end(arr1), [](double a) {cout << a << " ";});
  30.     cout << endl;
  31.    
  32.     std::for_each(std::begin(arr2), std::end(arr2), [](int a) {cout << a << " ";});
  33.     cout << endl;
  34.    
  35.     double neg_sum = std::accumulate(std::begin(arr1), std::end(arr1), 0, [](double a, double b) { return b < 0 ? a + b : a; });
  36.    
  37.     int pos_sum = std::accumulate(std::begin(arr2), std::end(arr2), 0, [](int a, int b) { return b > 0 ? a + b : a; });
  38.    
  39.     double min1, max1;
  40.     auto res1 = std::minmax_element(std::begin(arr1), std::end(arr1));
  41.     min1 = *res1.first;
  42.     max1 = *res1.second;
  43.    
  44.     int min2, max2;
  45.     auto res2 = std::minmax_element(std::begin(arr2), std::end(arr2), [](int a, int b) { return std::abs(a) < std::abs(b);});
  46.     min2 = *res2.first;
  47.     max2 = *res2.second;
  48.    
  49.     bool flag1 = false;
  50.     double prod1 = 1.0;
  51.     for(auto x : arr1){
  52.         if(x == min1 || x == max1){
  53.             flag1 = !flag1;
  54.             continue;
  55.         }
  56.         if(flag1){
  57.             prod1 *= x;
  58.         }
  59.     }
  60.    
  61.     bool flag2 = false;
  62.     double prod2 = 1.0;
  63.     for(auto x : arr2){
  64.         if(x == min2 || x == max2){
  65.             flag2 = !flag2;
  66.             continue;
  67.         }
  68.         if(flag2){
  69.             prod2 *= x;
  70.         }
  71.     }
  72.  
  73.     cout << neg_sum << " " << pos_sum << " " << prod1 << " " << prod2;
  74.  
  75.     return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement