Problem Description
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
一直都没怎么用过数据结构,这道题可以用数组,但想了想很符合栈,就用栈写了下
#include <iostream>
#include <cstdio>
#include <stack>
using namespace std;
int main()
{
int n;
while(~scanf("%d", &n))
{
stack<int> s;
while(n > 1)
{
s.push(n % 2);
n = n/2;
}
printf("1");
while(!s.empty())
{
printf("%d", s.top());
s.pop();
}
printf("\n");
}
}