#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<cmath>
#include<iostream>
#include<vector>
#include<set>
#include<queue>
#include<map>
using namespace std;
typedef long long LL;
const int N=1e5+10,mod=1e6;
const int MAX = 1e7 + 10 ;
char ch1[10010] , ch2[100101] ;
int num[1000] ;
void ten_to_two(){
int len = strlen(ch1) ;
int shang_sum = 1 ;
int flag = 0 ;
int a , b ;
while(shang_sum){
b = 0 ;
shang_sum = 0 ;
ch2[flag ++ ] = (ch1[len-1] - '0') % 2 + '0' ;
for(int i = 0 ; i < len; i++){
if(b == 0){
a = (ch1[i] - '0') ;
ch1[i] = a/2 + '0' ;
}else {
a = ch1[i] - '0' + 10 ;
ch1[i] = a/2 + '0' ;
}
if(a % 2 == 0) {
b = 0 ;
}else {
b = 1 ;
}
shang_sum += a / 2 ;
}
}
}
int two_to_ten(){
int len2 = 1 ;
num[0] = 0 ;
for(int i = 0 ; i < strlen(ch2) ; i++){
for(int j = 0 ; j < len2 ; j ++){
if(j == 0){
num[j] = num[j]*2 + ch2[i] - '0' ;
}else {
num[j] = num[j]*2 ;
}
}
for(int j = 0 ; j < len2 ; j++){
if(j == len2 - 1 && num[len2-1] >= 10){
num[j] = num[j] %10 ;
num[++j] = 1;
len2++ ;
}else if(num[j] >= 10){
num[j] = num[j]%10 ;
num[j+1] ++ ;
}
}
}
return len2 ;
}
int main(){
while(cin >> ch1){
memset(ch2,0,sizeof(ch2)) ;
ten_to_two() ;
int len2 = two_to_ten() ;
for(int i = len2 - 1; i >=0 ; i--){
cout << num[i] ;
}
cout <<endl;
}
}
看了这位大佬的分析:
https://blog.youkuaiyun.com/j754379117/article/details/40631899