进制转换
作者:
胡歌
,
2021-09-05 22:03:27
,
所有人可见
,
阅读 300
#include <bits/stdc++.h>
using namespace std;
bool find(vector<int> &a)
{
for(auto x:a)
{
if(x>0) return true;
}
return false;
}
vector<int> div(vector<int> &A,int &r) // A / b = C ... r, A >= 0, b > 0
{
int b=2;
vector<int> C;
for (int i = A.size() - 1; i >= 0; i -- )
{
r = r * 10 + A[i];
C.push_back(r / b);
r %= b;
}
reverse(C.begin(), C.end());
while (C.size() > 1 && C.back() == 0) C.pop_back();
return C;
}
string solve(string a)
{
string b="";
vector<int> s;
for(int i=a.size()-1;i>=0;i--) s.push_back(a[i]-'0');
while(find(s))
{
int r=0;
s=div(s,r);
b+=char(r+'0');
}
reverse(b.begin(),b.end());
if(b=="") return "0";
return b;
}
int main()
{
string a;
while(cin>>a)
{
cout<<solve(a)<<endl;
}
return 0;
}