笔试强训-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());
    }
}
相关推荐
Wendy14417 小时前
【线性回归(最小二乘法MSE)】——机器学习
算法·机器学习·线性回归
拾光拾趣录7 小时前
括号生成算法
前端·算法
渣呵8 小时前
求不重叠区间总和最大值
算法
拾光拾趣录8 小时前
链表合并:双指针与递归
前端·javascript·算法
好易学·数据结构8 小时前
可视化图解算法56:岛屿数量
数据结构·算法·leetcode·力扣·回溯·牛客网
香蕉可乐荷包蛋10 小时前
AI算法之图像识别与分类
人工智能·学习·算法
chuxinweihui10 小时前
stack,queue,priority_queue的模拟实现及常用接口
算法
tomato0910 小时前
河南萌新联赛2025第(一)场:河南工业大学(补题)
c++·算法
墨染点香10 小时前
LeetCode Hot100【5. 最长回文子串】
算法·leetcode·职场和发展
甄卷11 小时前
李沐动手学深度学习Pytorch-v2笔记【08线性回归+基础优化算法】2
pytorch·深度学习·算法