Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- #include <vector>
- using namespace std;
- string add_big_integer(string A, string B) { // funkcija koja ke ni sobere A + B
- if(A.size() < B.size()) {
- swap(A, B);
- }
- int a_i = (int) A.size() - 1;
- int b_i = (int) B.size() - 1;
- int carry = 0;
- string sum = "";
- while(a_i >= 0 and b_i >= 0) {
- int digit_a = (A[a_i] - '0');
- int digit_b = (B[b_i] - '0');
- int zbir = digit_a + digit_b + carry;
- if(zbir <= 9) {
- sum += (zbir + '0');
- carry = 0;
- }
- else {
- carry = 1;
- zbir -= 10;
- sum += (zbir + '0');
- }
- a_i--;
- b_i--;
- }
- while(a_i >= 0) {
- int digit_a = (A[a_i] - '0');
- int zbir = digit_a + carry;
- if(zbir <= 9) {
- sum += (zbir + '0');
- carry = 0;
- }
- else {
- carry = 1;
- zbir -= 10;
- sum += (zbir + '0');
- }
- a_i--;
- }
- if(carry == 1) {
- sum += "1";
- }
- reverse(sum.begin(), sum.end());
- return sum;
- }
- string multiply_big_integer(string A, string B) {
- int sz_a = (int) A.size();
- int sz_b = (int) B.size();
- vector<int> v(sz_a + sz_b, 0);
- for(int i = sz_a - 1; i >= 0; i--) {
- for(int j = sz_b - 1; j >= 0; j--) {
- v[i + j + 1] += (A[i] - '0') * (B[j] - '0');
- }
- }
- for(int i = sz_a + sz_b - 1; i >= 0; i--) {
- if(v[i] > 9) {
- v[i - 1] += (v[i] / 10);
- v[i] %= 10;
- }
- }
- int i = 0;
- while(v[i] == 0) {
- i++;
- }
- string product = "";
- for(int j = i; j < sz_a + sz_b; j++) {
- product += (v[j] + '0');
- }
- return product;
- }
- string divide_by_2(string s, int &rem) {
- int i = 0;
- int n = s.size();
- string result = "";
- int sum = 0;
- int tmp = (s[i] - '0');
- while(i < n) {
- if(tmp < 2) {
- tmp *= 10;
- tmp += (s[i + 1] - '0');
- i++;
- }
- result += (tmp / 2) + '0';
- int p = tmp / 2;
- rem = (tmp % 2);
- sum += (tmp - 2 * p);
- if(i + 1 < n)
- sum = sum * 10 + (s[i + 1] - '0');
- tmp = sum;
- i++;
- }
- if(add_big_integer(result, result) != s) rem = 1;
- return result;
- }
- int main() {
- string s;
- cin >> s;
- string decimal_number = "";
- string power_of_8 = "1";
- for(int i = (int) s.size() - 1; i >= 0; i--) {
- string a = "";
- a += s[i];
- decimal_number = add_big_integer(decimal_number, multiply_big_integer(a, power_of_8));
- power_of_8 = multiply_big_integer(power_of_8, "8");
- }
- cout << decimal_number << endl;
- string binary_number = "0";
- string power_of_10 = "1";
- while(isdigit(decimal_number[0])) {
- int rem = 1;
- string divided = divide_by_2(decimal_number, rem);
- string remainder = "0";
- if(rem == 1) {
- remainder = "1";
- }
- binary_number = add_big_integer(binary_number, multiply_big_integer(remainder, power_of_10));
- power_of_10 = multiply_big_integer(power_of_10, "10");
- decimal_number = divided;
- }
- cout << binary_number << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement