So it will not go (using the gmp library)?
#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 you do not use gmp, then rational numbers can be realized in pairs of integers (you just need to write operator >>, operator << and the reduction of fractions). Using float / double is fraught with rounding errors in the case of recurring fractions (the denominator is not a power of two). For example 10 80 10
answer 20 640/9 80/9