c++中10进制怎么转换为2进制?

2025-04-02 08:00:02
推荐回答(4个)
回答1:

//关于转换为2进制的进位计算
#include
#include
using namespace std;
void main()
{
stacks;
int n,a,m;
cout<<"输入一个数";
cin>>n;
cout<<"输入要转化的进制";
cin>>m;
cout<if(m{
while(n)
{
a=n%m;
n/=m;
if(a>9)
a=65+a-10-48;// 凑成英文字母的ASC值
s.push(a+48);//加上48后变成ASC的数字
}
}
else
{
while(n)
{
a=n%m;
n/=m;
if(a>9)
a=65+a-10-48;// 凑成英文字母的ASC值
s.push(a+48);//加上48后变成ASC的数字
}
s.push(48);//多输入一个0即可
}
while(!s.empty())
{cout<s.pop();
}
cout<}

回答2:

不断的除2求余,再把余数除2求余,直到除尽或余数为1停止
#include "iostream.h"
void main()
{
int y=0,yushu;
static int i=0;
int x;
cout<<"请输入十进制数:";
cin>>x;
int a[1000]={0};
if(x==0)
{
cout<<"转换的二进制数为:0";
}

while(x>0)
{

yushu=x%2;
x=x/2;
a[i++]=yushu;

}
cout<<"转换的二进制数为:";
for(y=i-1;y>=0;y--)
{
cout< }
cout<
}

回答3:

你是要以二进制形式输出还是怎么??

回答4:

每次除以2取余数