蓝桥杯-算法训练-审美课(Java)

对完全相反的审美进行异或操作的话就会得到全1,所以把每个人的审美存成二进制数字。把审美数据存到map里面,这样只需要一次遍历就可以得到答案了。

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String[] firstLine = scanner.nextLine().split(" ");
        int rows = Integer.parseInt(firstLine[0]);
        int cols = Integer.parseInt(firstLine[1]);

        HashMap<Integer, Integer> map = new HashMap<>();
        for(int i = 0; i < rows; i++){
            String line = scanner.nextLine().replaceAll(" ", "");
            int score = Integer.parseInt(line, 2);
            if(map.containsKey(score)){
                map.put(score, map.get(score) + 1);
            } else{
                map.put(score, 1);
            }
        }
        scanner.close();

        int ans = 0;
        int allOnes = (int) (Math.pow(2, cols) - 1);
        for(Iterator<Integer> iterator = map.keySet().iterator(); iterator.hasNext();){
            int key = iterator.next();
            int reverse = allOnes - key;
            if(map.containsKey(reverse)){
                ans += map.get(reverse) * map.get(key);
            }
        }

        System.out.println(ans / 2);
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值