问题标题: 酷町堂:快来人!!!!!!!!!!!!

0
0
已解决
汪宇航
汪宇航
新手启示者
新手启示者

题目描述 Description

现有两个整数a,b,将他们分别转为对应的二进制,请你计算两个数字对应的二进制有多少个对应位置上的数字不一样。

输入描述 Input Description

一行:两个整数啊a,b,空格隔开

输出描述 Output Description

一行:两个数字对应的二进制位不同的位置总数。

样例输入 Sample Input

1 4

样例输出 Sample Output

2

数据范围及提示 Data Size & Hint

【数据范围】
0≤a,b≤231

【样例说明】
image.png

WA0

#include <bits/stdc++.h>
using namespace std;
int a,b,x[11111],y[11111],cnt=0,cnt1=0,c=0,t=1;
void xxxx(int n){
    while(n){
        if(n&1){
            x[++cnt]=t;
        }
        n>>=1;
        t<<=1;
    }
}
void yyyy(int n){
    while(n){
        if(n&1){
            x[++cnt]=t;
        }
        n>>=1;
        t<<=1;
    }
}
int main(){
    memset(x,0,sizeof(x));
    memset(y,0,sizeof(y));
    cin>>a>>b;
    xxxx(a);
    yyyy(b);
    for(int i=1;i<=max(cnt,cnt1);i++){
        if(x[i]!=y[i])c++;
    }
    cout<<c;
    return 0;
}


0
已采纳
张展嘉
张展嘉
新手天翼
新手天翼
不需要函数
    d=a^b;
    while(d){
        if(d&1==1){
            c++;
        }
        d=d>>1;
    }

 

我要回答