題目分析:將某個數從十進制轉為二進制的具體方法是,該數對2取余,結果要么為1要么為0,此為該數對應二進制的末位;然后該數除以二,得到的商再次對2取余,結果為對應二進制的倒數第二位……以此類推,知道除以2的結果為0。
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
string s;
int main(int argc,char * argv[])
{
int n;
while(cin>>n)
{
if(n==0)
{
cout<<" 0-->0/n";
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<<"/n";
}
system("pause");
return 0;
}