Skip to content

Instantly share code, notes, and snippets.

@ybm
Created July 17, 2013 04:32
Show Gist options
  • Save ybm/6017715 to your computer and use it in GitHub Desktop.
Save ybm/6017715 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <stack>
int main( int argc, const char* argv[] ) {
unsigned int x;
stack<int> bin;
// 接受任何一个无符号整数,直到 EOF 为止
while ( std::cin >> x ) {
// 把十进制数依次转化为二进制并压入栈内
while ( x != 0 ) {
int b = x % 2;
bin.push(b);
x = x / 2;
}
// 依次输出栈内元素
while ( !bin.empty() ) {
std::cout << bin.top();
bin.pop();
}
cout << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment