笔试强训-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());
    }
}
相关推荐
历程里程碑4 小时前
Linux22 文件系统
linux·运维·c语言·开发语言·数据结构·c++·算法
你撅嘴真丑11 小时前
第九章-数字三角形
算法
uesowys11 小时前
Apache Spark算法开发指导-One-vs-Rest classifier
人工智能·算法·spark
ValhallaCoder12 小时前
hot100-二叉树I
数据结构·python·算法·二叉树
董董灿是个攻城狮12 小时前
AI 视觉连载1:像素
算法
智驱力人工智能12 小时前
小区高空抛物AI实时预警方案 筑牢社区头顶安全的实践 高空抛物检测 高空抛物监控安装教程 高空抛物误报率优化方案 高空抛物监控案例分享
人工智能·深度学习·opencv·算法·安全·yolo·边缘计算
孞㐑¥13 小时前
算法——BFS
开发语言·c++·经验分享·笔记·算法
月挽清风13 小时前
代码随想录第十五天
数据结构·算法·leetcode
XX風13 小时前
8.1 PFH&&FPFH
图像处理·算法
NEXT0613 小时前
前端算法:从 O(n²) 到 O(n),列表转树的极致优化
前端·数据结构·算法