题解 | #10进制 VS 2进制#
10进制 VS 2进制
https://www.nowcoder.com/practice/fd972d5d5cf04dd4bb4e5f027d4fc11e
#include <iostream>
#include<vector>
#include <string>
using namespace std;
string divide(string str){
int remain=0;
for(int index = 0;index<str.size();index++){
int current =remain*10+str[index]-'0';
str[index]=current/2+'0';
remain =current%2;
}
int i=0;
while(str[i]=='0')i++;
return str.substr(i);
}
string multipy(string str){
int jw =0;
for(int i = str.size()-1 ; i>=0;i--){
int current=(str[i]-'0')*2+jw;
jw=current/10;
current%=10;
str[i]=current+'0';
}
if(jw!=0){
char j = '0'+jw;
str=j+str;
}
return str;
}
string reverse(string str){
string reversestr;
for(int i = 0 ; i< str.size();i++){
reversestr+=str[i];
}
int index=0;
while(reversestr[index]=='0')index++;
return reversestr.substr(index);
}
string TenToTwo(string str){
vector<char> ans;
while(str.size()!=0){
ans.push_back((str[str.size()-1]-'0')%2+'0');
str=divide(str);
}
string a;
for(int i = 0 ; i<ans.size();i++)a+=ans[i];
return a;
}
string TwoToTen(string str){
string s;
s+=str[0];
for(int i = 1 ; i < str.size();i++){
s=multipy(s);
s[s.size()-1]+=str[i]-'0';
}
return s;
}
int main() {
string str;
while(getline(cin,str)){
cout<<TwoToTen(reverse(TenToTwo(str)));
}
return 0;
}
// 64 位输出请用 printf("%lld")

