Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <string>
- using namespace std;
- int input_int(const string msg, const int mn, const int mx);
- int main() {
- int n = input_int(" n: ", 2, 200);
- vector<int> p(n);
- int i = 0;
- do p.at(i) = input_int(" weight: ", 1, 1000); while (++i < n);
- sort(p.begin(), p.end());
- reverse(p.begin(), p.end());
- int h1 = 0, h2 = 0;
- i = 0;
- do {
- h1 += p.at(i);
- while (h1 > h2 && i++ < n) h2 += p.at(i);
- } while (++i < n);
- int d = h1 > h2 ? h1 - h2 : h2 - h1;
- cout << " heap1: " << h1 << ", heap2: " << h2 << " diff: " << d << endl;
- cin.sync();
- cin.get();
- return 0;
- }
- int input_int(const string msg, const int mn, const int mx) {
- int i;
- do {
- cout << msg;
- cin >> i;
- if (cin.good() && i < mx || i >= mn) return i;
- else {
- cin.clear();
- cin.ignore(cin.rdbuf()->in_avail());
- cout << "\a Error!\n";
- }
- } while (true);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement