笔试强训-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方式~

三、答案解析
算法思路
模拟即可,但是由于数据量过⼤,我们可以提前把每⼀⾏以及每⼀列的和存起来,⽅便统计总和。
编写代码
解法一:暴力遍历,这种方法很容易想到,并且也容易编写出代码,但是会超时

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方式,解答如下所示


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());
    }
}

相关推荐

  1. 笔试-day14_T2 组队竞赛

    2024-05-04 02:56:01       33 阅读
  2. 笔试-day14_T1 乒乓球筐

    2024-05-04 02:56:01       34 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-04 02:56:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-04 02:56:01       101 阅读
  3. 在Django里面运行非项目文件

    2024-05-04 02:56:01       82 阅读
  4. Python语言-面向对象

    2024-05-04 02:56:01       91 阅读

热门阅读

  1. 力扣-977.有序数组的平方

    2024-05-04 02:56:01       30 阅读
  2. PostgreSQL的扩展pgpool

    2024-05-04 02:56:01       34 阅读
  3. pyflink filter

    2024-05-04 02:56:01       27 阅读
  4. 【AI学习】人工智能 or 人造智能 or 人创智能

    2024-05-04 02:56:01       33 阅读
  5. 你用过最好用的AI工具有哪些?【模板】

    2024-05-04 02:56:01       29 阅读
  6. React 之 如何启动一个新的项目(六)

    2024-05-04 02:56:01       31 阅读
  7. python - mac安装mysqlclient

    2024-05-04 02:56:01       26 阅读