该题是一道填空题,但是题目却这么长,相信很多人一开始都被题目长度吓到了,不过仔细审完题之后发现该题考察的就是“十进制转二进制”
我主要使用的是位运算来做这道题,当然也可以用“模拟”的方法
不过毕竟数字在计算机中保存的方式本来就是二进制,还是使用位运算更为方便
#include<iostream>
using namespace std;
int main()
{
int n,m;
int num[20];
while (cin >> n >> m)
{
for (int i = 7; i >= 0; i--)
{
num[i] = n & 1;//将数字的二进制中的1挑出来
n >>= 1;
}
for (int j = 15; j >= 8; j--)
{
num[j] = m & 1;
m >>= 1;
}
for (int i = 0; i <= 15; i++)
{
if (num[i])
{
cout << num[1];
}
else
{
cout << ' ';
}
}
cout << endl;
}
return 0;
}