#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int j = 0;//用于记录每个二进制位(倒叙)
int s = 0;//记录输入的数
int arr[256] = { 0 };
printf("请输入数字\n");
scanf("%d",&s);
int tmp = s;
int i = 1;//7 3 1
while (s >= 1)
{
++i;
s = s / 2;
if (s == 1)
{
break;
}
}
for (int j = 0; j < i; j++)//11 arr[0]=1 arr[1]=1 arr[2]=0;
{
arr[j] = tmp % 2; //11 5.1 5; 5 2.1 2; 2 1.0 ; 1 0.1 ;
tmp = tmp / 2;
}
printf("其二进制为:\n");
for (int k=i-1; k>=0; k--)
{
printf("%d",arr[k]);
}
return 0;
}
标签:tmp,10,arr,进制,二进制,++,int,printf,11
From: https://blog.51cto.com/u_16425777/8946904