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) {
- string result = "";
- int tmp = (s[0] - '0');
- int i =0;
- while(tmp < 2) {
- tmp = (tmp * 10) + (s[++i] - '0');
- }
- while(s.size() > i) {
- result += (tmp / 2) + '0';
- tmp = (tmp % 2) * 10 + s[++i] - '0';
- }
- if(add_big_integer(result, result) != s) {
- rem = 1;
- }
- if(result.size() == 0) {
- return "0";
- }
- 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(decimal_number != "0") {
- int rem = 0;
- 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");
- cout << divided << endl;
- decimal_number = divided;
- }
- cout << binary_number << endl;
- return 0;
- }
- // 101101101101101101101101101
- // 101101101101101101101101101
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement