using namespace std; bitset<100>grath; int main() { int t;cin>>t; while(t--){ int a,b; cin>>a>>b; grath=a^b; cout<<grath.count()<<endl; } }
我觉得有个要注意的地方,如果输入的x和y位数长短不一致,比如x比y位数长。y的高位要补0再和x比,x的高位中的1视作和y不一致的位。
瑞哥又来送答案啦
int main() { int t,x,y,z,q; scanf(“%d”,&t); while(t–) { scanf(“%d %d”,&x,&y); z=0; for(q=0;q<32;q++) { if((x%2)!=(y%2)) z++; x>>=1; y>>=1; } printf(“%d\n”,z); } return 0; } [em:02]
我觉得位运算最省事
int main() { int i,t; scanf(“%d”,&t); for(i=0;i<t;i++) { long int a,b,c; int n=0; scanf(“%ld %ld”,&a,&b); c=a^b; while(c!=0) { n+=c%2; c/=2; } printf(“%d\n”,n); } return 0; }
using namespace std; int main() { int T; cin >> T; while(T–){ int x, y; cin >> x >> y; int cnt = 0, z = x ^ y; while(z){ cnt += z % 2; z = z >> 1; } cout << cnt << endl; } }
include
我觉得有个要注意的地方,如果输入的x和y位数长短不一致,比如x比y位数长。y的高位要补0再和x比,x的高位中的1视作和y不一致的位。
瑞哥又来送答案啦
include
include
int main()
{
int t,x,y,z,q;
scanf(“%d”,&t);
while(t–)
{
scanf(“%d %d”,&x,&y);
z=0;
for(q=0;q<32;q++)
{
if((x%2)!=(y%2))
z++;
x>>=1;
y>>=1;
}
printf(“%d\n”,z);
}
return 0;
}
[em:02]
我觉得位运算最省事
include
int main()
{
int i,t;
scanf(“%d”,&t);
for(i=0;i<t;i++)
{
long int a,b,c;
int n=0;
scanf(“%ld %ld”,&a,&b);
c=a^b;
while(c!=0)
{
n+=c%2;
c/=2;
}
printf(“%d\n”,n);
}
return 0;
}
include
using namespace std;
int main()
{
int T;
cin >> T;
while(T–){
int x, y;
cin >> x >> y;
int cnt = 0, z = x ^ y;
while(z){
cnt += z % 2;
z = z >> 1;
}
cout << cnt << endl;
}
}