hdu2051(stack)

xiaoxiao2021-02-27  544

Give you a number on base ten,you should output it on base two.(0 < n < 1000) Input For each case there is a postive number n on base ten, end of file. Output For each case output a number on base two. Sample Input 1 2 3 Sample Output 1 10 11

/*题意将一个10进制数n按照二进制书输出出来

思路:直接加入栈当n == 1时跳出并输出即可*/

#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <stack> using namespace std; int main() { int n; while(scanf("%d", &n) != EOF){ stack <int >q; while(1){ int a; a = n % 2; q.push(a); if(n == 1) break; n /= 2; } while(!q.empty()){ printf("%d", q.top()); q.pop(); } printf("\n"); } return 0; }

转载请注明原文地址: https://www.6miu.com/read-1424.html

最新回复(0)