将十进制整数转换成二进制数
对于每个n,以11位的宽度右对齐输出n值,然后输出"-->",然后输出二进制数。
输入样例:
2
0
-12
1
输出样例:
2-->10
0-->0
-12-->-1100
1-->1
#include<iostream>
#include<fstream>
#include<iomanip>
#include<stack>
using namespace std;
int main()
{
ifstream cin("test.txt");//向OJ提交时,注释此句
int n;
while (cin >> n)
{
cout << setw(11) << n << "-->";
bool flag = n < 0 ? true : false;
n = flag ? n*-1 : n;
stack<int> s;
while (n)
{
s.push(n % 2);
n /= 2;
}
if (s.empty())
cout << 0;
else
{
if (flag)
cout << "-";
while (!s.empty())
{
int tmp = s.top();
s.pop();
cout << tmp;
}
}
cout << endl;
}
system("pause");//向OJ提交时,注释此句
return 0;
}