본문 바로가기
Algorithm 💫/Problem Solving

[백준] 3449번: 해밍 거리

by 돼지고기맛있다 2021. 10. 4.
반응형

✏️ 문제 링크

https://www.acmicpc.net/problem/3449

 

3449번: 해밍 거리

입력을 여러 개의 테스트 케이스로 이루어져 있다. 첫째 줄에는 테스트 케이스의 개수 T가 주어진다. 각 테스트 케이스는 두 줄로 이루어져 있다. 각 줄에는 이진수가 하나씩 주어진다. 두 이진

www.acmicpc.net

 

 

✏️ 문제 설명 (더보기 클릭 👆🏻)

 

✏️ 문제 풀이

각 A, B를 XOR 연산을 하면 풀 수 있는 문제...인데!! C++로 비트다루는건 힘들다...ㅎㅅㅎ 

 

 

✏️ 문제 코드

#include <bits/stdc++.h>

using namespace std;

int main(){
    int T; cin>>T;
    
    while(T--){
        string a, b; cin>>a>>b;
        int n=a.size();
        
        long A=stol(a, 0, 2);
        long B=stol(b, 0, 2);
        
        long X=A ^ B;
        
        long cnt=0;
        for(int i=0; i<n; i++){
            cnt+=((X>>i) & 1);
        }
        
        cout<<"Hamming distance is "<<cnt<<".\n";
    }
    return 0;
}

 

 

 

 ⭐ if feedback and question : comment please⭐  

 

 

 

 

 

반응형

댓글