大数进制转换
大数进制转换
算法思想:浅谈大数的进制转换
代码:
#include <stdio.h>
#include <string.h>
int main() {
char m[32];
int len;
while (scanf("%s", &m) != EOF) {
len = strlen(m); //记录输入长度
int sum = 1, size = 0;
char n[100]; //用来存储二进制数
while (sum) {
sum = 0; //判断循环跳出条件
//逐位除以2
for (int i = 0; i < len; i++) {
int x = m[i] - '0';
int d = x / 2;
sum += d;
if (i < len - 1) {
m[i + 1] = x % 2 * 10 + m[i + 1];
}
if (i == len - 1) {
n[size++] = x % 2 + '0';
}
m[i] = d + '0';
}
}
for (int i = size - 1; i >= 0; i--) {
printf("%c", n[i]);
}
printf("\n");
}
return 0;
}
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.