笔试强训-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());
    }
}
相关推荐
梨子串桃子_6 小时前
推荐系统学习笔记 | PyTorch学习笔记
pytorch·笔记·python·学习·算法
夏鹏今天学习了吗6 小时前
【LeetCode热题100(83/100)】最长递增子序列
算法·leetcode·职场和发展
情缘晓梦.6 小时前
C语言指针进阶
java·开发语言·算法
北邮刘老师7 小时前
智能体治理:人工智能时代信息化系统的全新挑战与课题
大数据·人工智能·算法·机器学习·智能体互联网
AlenTech7 小时前
155. 最小栈 - 力扣(LeetCode)
算法·leetcode·职场和发展
mit6.8248 小时前
正反两次扫描|单调性cut
算法
Yzzz-F8 小时前
牛客小白月赛127 E
算法
大锦终8 小时前
递归回溯综合练习
c++·算法·深度优先
Keep__Fighting8 小时前
【神经网络的训练策略选取】
人工智能·深度学习·神经网络·算法
晚风吹长发8 小时前
初步了解Linux中的动静态库及其制作和使用
linux·运维·服务器·数据结构·c++·后端·算法