笔试强训-day17_T2 十字爆破

一、题目链接
十字爆破

二、题目描述

牛牛在玩一个游戏:

一共有n行m列共nm个方格,每个方格中有一个整数。
牛牛选择一个方格,可以得到和这个方格同行、同列的所有数之和的得分。
例如:对于一个2
2的方格:

1 2

3 4

牛牛选择每个方格的得分如下:

6 7

8 9

因为1+2+3=6,1+2+4=7,1+3+4=8,2+3+4=9。

现在牛牛想知道下一步选择每个格子的得分情况,你可以帮帮他吗?
输入描述:

输出描述:

备注

本题输入和输出数据量较大,尽量使用scanf或更快的IO方式~

三、答案解析
算法思路

模拟即可,但是由于数据量过⼤,我们可以提前把每⼀⾏以及每⼀列的和存起来,⽅便统计总和。
编写代码

解法一:暴力遍历,这种方法很容易想到,并且也容易编写出代码,但是会超时

java 复制代码
import java.util.Scanner;

public class Test2 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        long[][] pes = new long[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                pes[i][j] = in.nextLong();
            }
        }
        long[][] ret = new long[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                long sum = 0;
                for (int k = 0; k < n; k++) {
                    sum += pes[k][j];
                }
                for (int k = 0; k < m; k++) {
                    sum += pes[i][k];
                }
                ret[i][j] = sum - pes[i][j];
                System.out.print(ret[i][j] + " ");
            }
            System.out.println();
        }

    }
}

解法二:提前把每⼀⾏以及每⼀列的和存起来,然后输入输出使用快速的IO方式,解答如下所示

java 复制代码
import java.util.*;
import java.io.*;

public class Main {
    public static Read in = new Read();
    public static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
   
    public static void main(String[] args) throws IOException{
        int n = in.nextInt();
        int m = in.nextInt();
        long[][] arr = new long[n][m];
        long[] row = new long[n];
        long[] col = new long[m];

        for(int i = 0;i<n;i++){
            for(int j = 0;j<m;j++){
               arr[i][j] = in.nextInt();
                row[i] +=arr[i][j];
                col[j]+=arr[i][j];
            }
        }
        
        for(int i = 0;i<n;i++){
            for(int j=0;j<m;j++){
                out.print((row[i]+col[j]-arr[i][j])+" ");
            }
            out.println();
        }
        out.close();
    } 
}



class Read // 自定义快速读入
{
    StringTokenizer st = new StringTokenizer("");
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));

    String next() throws IOException {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(bf.readLine());
        }
        return st.nextToken();
    }

    String nextLine() throws IOException {
        return bf.readLine();
    }

    int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }
}
相关推荐
qq_513970446 分钟前
力扣 hot100 Day37
算法·leetcode
不見星空26 分钟前
leetcode 每日一题 1865. 找出和为指定值的下标对
算法·leetcode
我爱Jack36 分钟前
时间与空间复杂度详解:算法效率的度量衡
java·开发语言·算法
DoraBigHead2 小时前
小哆啦解题记——映射的背叛
算法
Heartoxx2 小时前
c语言-指针与一维数组
c语言·开发语言·算法
孤狼warrior3 小时前
灰色预测模型
人工智能·python·算法·数学建模
京东云开发者3 小时前
京东零售基于国产芯片的AI引擎技术
算法
chao_7894 小时前
回溯题解——子集【LeetCode】二进制枚举法
开发语言·数据结构·python·算法·leetcode
十盒半价4 小时前
从递归到动态规划:手把手教你玩转算法三剑客
javascript·算法·trae
GEEK零零七4 小时前
Leetcode 1070. 产品销售分析 III
sql·算法·leetcode