Jennifer
2018年2月2日17:09:12-2018年2月2日17:50:53
题目内容:
将十进制整数转换为二进制数
输入描述:输入数据中含有不多于50个的整数n
输出描述:对于每个n,以11位的宽度右对齐输出n值,然后输出"-->",再然后输出二进制数。每个整数n的输出,独立占一行。
输入样例:
2
0
-12
1
输出样例:
2-->10
0-->0
-12-->-1100
1-->1
*/
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
string s;
int main()
{
int n;
while(cin>>n)
{
if(n == 0)
{
cout<<" 0-->0"<<endl;
continue;
}
s = "";
for(int a=n;a;a=a/2)
{
s = s + (a%2?'1':'0');
}
std::reverse(s.begin(),s.end());
const char *sss = s.c_str();
cout.width(11);
cout<<n<<(n<0?"-->-":"-->")<<sss<<endl;
}
return 0;
}