#include <cstdio>
#include <iostream>
#include <algorithm>
#include <map>
#include <cstring>
using namespace std;
/*************************************************
trip0函数在程序中比较关键的作用是在除二运算后,去除
高位的0,方便进行比较。
*************************************************/
void trip0(string &s){
while(!s.empty() && s[0] == '0')
s.erase(s.begin());
}
string div_two(string s){
string ans;
int plus = 0;
/*******************************************
大数除法的部分
通过小时候所学习的基本的运算法则:通过除数除
被除数的每一位 ,由高到低进行运算。余数到下
一位。plus是余数。now是当前位的被除数。在编写
程序的时候出现了一个问题,程序一直不运算完成,
通过调试发现是由于plus未初始化,导致其中有一个
很大的数。
********************************************/
for(int i = 0;i < s.length();i ++){
int now = plus * 10 + (s[i] - '0');
if(now >= 2){
ans.push_back(char(now / 2 + '0'));
plus = now % 2;
}else{
ans.push_back('0');
plus = now;
}
}
trip0(ans);
return ans;
}
int main(){
string tmp;
while(cin >> tmp){
string ans;
trip0(tmp);
while(!tmp.empty()){
//此处的写法值得借鉴。通过指针的访问,实现起来十分的灵活。
int rail = (*(tmp.end() - 1) - '0') % 2;
//string类型字符串的使用方式push_back
ans.push_back(char(rail + '0'));
tmp = div_two(tmp);
}
reverse(ans.begin(),ans.end());
if(!ans.empty()) cout << ans <<endl;
else cout<< 0 << endl;
}
return 0;
}
进制转换