#include <iostream> #include <stack> using namespace std; //等同于在纸上实现除法的过程 string Divide(string str,int x) { int remainder=0;//余数 for(int i=0;i<str.size();i++) { int current=remainder*10+str[i]-'0'; remainder=current%x; str[i]=current/x+'0'; } int j=0;//str前面有几个0 while(str[j]=='0') { j++; } return str.substr(j,str.size()-j);// //return str.substr(j);//从当前位置开始,返回后续所有子串 } int main() { string str; while (cin >> str) { stack<int> sta; while(str.size()!=0) { int size=str.size(); sta.push((str[size-1]-'0')%2); str=Divide(str,2); } //while(sta.top()==0)sta.pop();会引发段错误,因为栈为空的时候top()已经指不了东西了,更别说等于整数0了 while(!sta.empty()) { cout<<sta.top(); sta.pop(); } cout<<endl; } }