Created
July 17, 2013 04:32
-
-
Save ybm/6017715 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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