自创的大数进制转换模板,效率不高,但是比较容易懂。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> bas_change(vector<int> A, int ob, int nb)
{
vector<int> C; //商
vector<int> D; //转换后的结果
if (A.size() == 1 && A[0] == 0) //0直接返回
return {0};
while (A[0] != 0 || A.size() > 1) //当除成0的时候退出
{
int r = 0;
for (int i = A.size() - 1; i >= 0; --i)
{
r = r * ob + A[i];
C.push_back(r / nb);
r %= nb;
}
reverse(C.begin(), C.end());
while (C.size() > 1 && C.back() == 0) //去除前导0
C.pop_back();
A = C;
C.clear();
D.push_back(r);
}
return D;
}
int main()
{
string a;
while (cin >> a)
{
vector<int> b;
for (int i = a.size() - 1; i >= 0; --i)
b.push_back(a[i] - '0');
auto res = bas_change(b, 10, 2);
for (int i = res.size()- 1; i >= 0; --i)
cout << res[i];
cout << endl;
}
return 0;
}