大数进制转换

2022-12-26 15:29:27 浏览数 (1)

处理大数进制

将一个长度最多为30位数字的十进制非负整数转换为二进制数输出

代码语言:javascript复制
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;

string conversion(int m, string a, int n) {
 int l = a.length(), k;
 string b = "";
 for (int i = 0; i < l; ) {
  //塞入数字
  k = 0;
  //求商求余
  for (int j = i; j < l; j  ) {
    int t = (k * m   a[j] - '0') % n;
   a[j] = (k * m   a[j] - '0') / n   '0';
   k = t;
  }
  b  = char(k   '0');
  while (a[i] == '0') i  ;
 }
 reverse(b.begin(), b.end());        //反过来
 return b;
}


int main() {
 string a, b;
 while (cin >> a) {
  b = conversion(10, a, 2);
  cout << b << endl;
 }
 return 0;
}

0 人点赞