Advertisement
NickAndNick

Задача с камнями

Nov 3rd, 2014
656
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <string>
  5. using namespace std;
  6. int input_int(const string msg, const int mn, const int mx);
  7. int main() {
  8.     int n = input_int(" n: ", 2, 200);
  9.     vector<int> p(n);
  10.     int i = 0;
  11.     do p.at(i) = input_int(" weight: ", 1, 1000); while (++i < n);
  12.     sort(p.begin(), p.end());
  13.     reverse(p.begin(), p.end());
  14.     int h1 = 0, h2 = 0;
  15.     i = 0;
  16.     do {
  17.         h1 += p.at(i);
  18.         while (h1 > h2 && i++ < n) h2 += p.at(i);
  19.     } while (++i < n);
  20.     int d = h1 > h2 ? h1 - h2 : h2 - h1;
  21.     cout << " heap1: " << h1 << ", heap2: " << h2 << " diff: " << d << endl;
  22.     cin.sync();
  23.     cin.get();
  24.     return 0;
  25. }
  26. int input_int(const string msg, const int mn, const int mx) {
  27.     int i;
  28.     do {
  29.         cout << msg;
  30.         cin >> i;
  31.         if (cin.good() && i < mx || i >= mn) return i;
  32.         else {
  33.             cin.clear();
  34.             cin.ignore(cin.rdbuf()->in_avail());
  35.             cout << "\a Error!\n";
  36.         }
  37.     } while (true);
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement