本帖最后由 Whitehat_K 于 2016-5-13 12:32 编辑 #include <stdio.h>
int main ()
{
int change(int str_1[],int n);
int a, i, str[10], j;
printf ("请输入一个整数:");
scanf ("%d",&a);
if ( a > 0 )
{
j = change(str,a);
for (i = j-1; i >= 0; i--)
printf ("%d",str[i]);
printf ("\n");
}
else if( a < 0)
{
a = -a-1;
/*原理:http://blog.csdn.net/stpeace/article/details/43715781*/
j = change(str, a);
for (i = j-1; i >= 0; i--)
printf ("%d",!str[i]);
printf("\n");
}
return 0;
}
int change(int str_1[],int n)
{
int j = 0;
while (n != 0)
{
str_1[j++] = n%2;
n /= 2;
}
return j;
}
|