Advertisement
Josif_tepe

Untitled

Mar 3rd, 2022
1,212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #include <algorithm>
  5. using namespace std;
  6. int from_binary_to_decimal(string s) {
  7.     int power_of_two = 1;
  8.     int sum = 0;
  9.     for(int i = s.size() - 1; i >= 0; i--) {
  10.         if(s[i] == '1') {
  11.             sum += power_of_two;
  12.         }
  13.         power_of_two *= 2;
  14.     }
  15.     return sum;
  16. }
  17. int main()
  18. {
  19.     ios_base::sync_with_stdio(false);
  20.     cout.tie(0);
  21.     cin.tie(0);
  22.     cout << from_binary_to_decimal("101") << " " << from_binary_to_decimal("1");
  23.     return 0;
  24.  
  25. }
  26.  
  27.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement