中易网

编写一个程序,知道一个数的二进制表示中有多少个0?

答案:3  悬赏:0  
解决时间 2021-03-23 05:47
编写一个程序,知道一个数的二进制表示中有多少个0?
最佳答案
DEV-CPP  5.4  C通过编译,输入一个10进制输出其2进制,并统计有几个0
#include<stdio.h>
int main(){
    int b;char a[64*8];int i=0;
    scanf("%d",&b);
    if(b<0) putchar('-');
    while(b!=0){
    a[i]=b%2+48;
    b=b/2;
    i++;
    }
    char *s=a,*e=&a[i-1],t;
    while(e>s){
        t=*s;*s=*e;*e=t;s++;e--;
    }
    int n,sum=0;
    for(n=0;n<i;n++){
        putchar(a[n]);
        if(a[n]=='0') sum++;
    }
    printf("\nsum=%d",sum);
    return 0;
}
全部回答
#include<stdio.h> void main() { int x,s; scanf("%d",&x); s=0; while ( x ) { if ( x%2==0 ) s++; x/=2; } printf("%d\n",s); }
dev-cpp  5.4  c通过编译,输入一个10进制转换成2进制,并统计有几个0 #include int main(){ int b;char a[64*8];int i=0; scanf("%d",&b); if(b<0) putchar('-'); while(b!=0){ a[i]=b%2+48; b=b/2; i++; } char *s=a,*e=&a[i-1],t; while(e>s){ t=*s;*s=*e;*e=t;s++;e--; } int n,sum=0; for(n=0;n
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯