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
涉及:基础数学 - 代数 - 进位计数制,取模,栈
栈:先进后出特点
进制转换:10进制->2,将整数不停的除以2,取余数,直到整数变为0。得到的余数序列的要从后往前读才是正确的答案,栈的先进后出很好的应用
栈的使用:常用函数push(数值):插入数据,pop():删除数据,top():取栈顶元素,empty():判空
#include <iostream>
using namespace std;
#include <stack>
int main()
{
int x;
while (cin >> x)
{
stack<int> st;
while (x)
{
st.push(x % 2);
x = x / 2;//取整数部分
}
while (!st.empty())
{
cout << st.top();
st.pop();
}
cout << endl;
}
return 0;
}