Equally distributed reading. Simple task.



  • There are 3 numbers (in fact, they may not be one hundred, for example)

    10, 80, 10

    In sum, they must be 100, may be fragmented. If the first increase is 10, it'll be 20.

    20, x, y

    x and y in sum shall be 80 and the ratio of final numbers x and y shall remain.



  • That's not gonna happen.

    #include <gmp.h>
    #include <gmpxx.h>
    using namespace std;
    

    int main() {
    string str;
    vector<mpq_class> v;
    stringstream ss (stringstream::in | stringstream::out);
    mpq_class add, sum = 0, ratio;
    vector<mpq_class>::iterator i;
    getline(cin, str);
    ss.str(str);
    while(!ss.eof()) {
    mpq_class w;
    ss >> w;
    v.push_back(w);
    }
    v.pop_back(); // почему-то вводится лишнее число
    cin >> add;
    for(i = v.begin() + 1; i != v.end(); i++) sum += *i;
    ratio = (sum - add)/sum;
    cout << v[0] + add;
    for(i = v.begin() + 1; i != v.end(); i++) cout << ' ' << *i * ratio;
    cout << endl;
    return 0;
    }

    Translation g++ jkeks.c++ -lgmpxx -lgmp If gmp is not used, rational numbers can be achieved by couples of wholes (only the operator line, operator repair and shrink reduction). The use of float/double is liable to rounding errors in the case of recurrent throbes (the denominator is not a double).
    For example 10 80 10 Response 20 640/9 80/9


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2