/*
写一个函数,对给定整数的二进制表示进行描述
如:给定整数131,其二进制表示为10000011,要求函数输出以下结果:
1: 2
0: 5
1: 1
表示从最低位开始,包含2个1,5个0,1个1。
参考上一题,确定本函数的名字,入口出口及返回值,并实现本函数
*/
#include"stdio.h"
void result(int a)
{
int count1=0,count2=0;
int b;
bool is1=false;
bool is0=false;
while(1)
{
b=a%2;
if(b==1)
{
count1++;
if(count2!=0)
{
is0=true;
}
}else
{
count2++;
if(count1!=0)
{
is1=true;
}
}
if(is1)
{
is1=false;
printf("1:%d/n",count1);
count1=0;
}
if(is0)
{
is0=false;
printf("0:%d/n",count2);
count2=0;
}
if(a==0)break;
a=a/2;
}
}
void main()
{
int a;
printf("please input an integer:/n");
scanf("%d",&a);
result(a);
}