[비트연산] 소프티어 lv.3 CPTI [Java]

알고리즘/java 2025. 4. 7. 21:40
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.math.BigDecimal;

public class Main {

	public static void main(String[] args) throws NumberFormatException, IOException {
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		String[] nm = br.readLine().split(" ");
		
		int n = Integer.parseInt(nm[0]);
		
		int[] arr = new int[n];
		
		for (int i = 0; i < n; i++) {
			
			String d = br.readLine();
			arr[i] = Integer.parseInt(d, 2);
		}
		
		System.out.println(checkPairs(arr));
		
	}
	
	
	
    public static int checkPairs(int[] binaries) {
        int n = binaries.length;
        int result = 0;
        for (int i = 0; i < n - 1; i++) {
            for (int j = i + 1; j < n; j++) {
                int diffCount = Integer.bitCount(bin1 ^ bin2);
                if(diffCount < 3) {
                	result++;
                }
            }
        }
        return result;
    }
	
}

 

https://softeer.ai/practice/11002

 

비트연산으로 풀면 쉽게 풀릴 거라고 생각했으나

bitCount 함수를 떠올리지 못해서 생각보다 애먹었던 문제