Skip to content

Instantly share code, notes, and snippets.

@enedil
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enedil/fc9a1ef01bfab7cd07c2 to your computer and use it in GitHub Desktop.
Save enedil/fc9a1ef01bfab7cd07c2 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cmath>
int to_decimal(int n, int num);
int from_decimal(int n, int num);
int main()
{
std::ios_base::sync_with_stdio(0);
int P;
std::cin >> P;
std::cout << from_decimal(to_decimal(P, 2), 2) << "\n";
}
int to_decimal(int n, int num)
{
int res = 0;
for (int i = 0; n; i++)
{
res += (n % 10) * pow(num, i);
n /= 10;
}
return res;
}
int from_decimal(int n, int num)
{
int res = 0;
for (int i = 0; n; i++)
{
res += (n % num) * pow (10, i);
n /= num;
}
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment