#include #include #include using namespace std; int todec(string &s, int R) { int res = 0; for (unsigned int i = 0; i < s.length(); i++) { res = res * (-R) + (s[i] - '0'); } return res; } int mensi(int x, int R) { while (x%R != 0) x--; return x; } int vetsi(int x, int R) { while (x%R != 0) x++; return x; } string toneg(int x, int R) { int w, roz; bool min = true; string s; if (x == 0) return "0"; while (x != 0) { if (min) w = mensi(x, R); else w = vetsi(x, R); s = (char)('0' + abs(x-w)) + s; x = w/R; min = !min; } return s; } int main() { int base, numi; string nums; bool from; char c; cin >> c; while (c != 'e') { from = (c == 'f'); while (c != '-') cin >> c; cin >> base; if (!from) { cin >> numi; cout << toneg(numi, base) << endl; } else { cin >> nums; cout << todec(nums, base) << endl; } cin >> c; while (c < 'a' || c > 'z') cin >> c; } return 0; }