【算法】小美的平衡矩阵

题目描述

在这里插入图片描述

一、暴力解

c了20%,会超时,请大佬们看看哪里还能优化

public class Main {
    static int[] result;
    public static void main(String[] args) {
        //拿数据
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        String[] strs = new String[n];
        result = new int[n];
        for (int i = 0; i < n; i++) {
            strs[i] = sc.next();
        }
        //计算完美矩阵数
        for (int i = 1; i <= n; i++) {
            perfect(strs, i);
        }
        //输出结果
        for (int i = 0; i < result.length; i++) {
            System.out.println(result[i]);
        }
    }

    public static void perfect(String[] strs, int n) {
        //只有偶数需要计算
        if (n % 2 == 0) {
            //以(i,j)作为起始位置,扫(i,j)->(i + n, j + n)范围
            for (int i = 0; i <= strs.length - n; i++) {
                for (int j = 0; j <= strs.length - n; j++) {
                    int count_1 = 0;
                    for (int x = i; x < i + n; x++) {
                        for (int y = j; y < j + n; y++) {
                            if (strs[x].charAt(y) == '1') count_1++;
                        }
                    }
                    //计算1的数量
                    if(count_1 == (n * n) / 2) result[n - 1]++;
                }

            }
        }

    }
}

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值