#include<iostream>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std;
int main()
{
 int n;
 while(cin>>n)
 {
  if(n==0)
  {  cout<<"     0-->0\n";
      continue;
  }
  string   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";
 }
}

实现结果:

C++实现十进制转二进制

相关文章:

  • 2021-08-11
  • 2022-12-23
  • 2021-10-29
  • 2021-10-16
  • 2021-12-13
猜你喜欢
  • 2021-05-30
  • 2022-12-23
  • 2021-10-17
  • 2022-12-23
  • 2022-01-09
  • 2021-12-02
  • 2021-11-01
相关资源
相似解决方案