题解 | 多组_二维数组_T组形式

多组_二维数组_T组形式

https://www.nowcoder.com/practice/fe1e902e076b46dd82e7d42e12617893

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(System.out);

        int t = Integer.parseInt(br.readLine().trim()); // 读取组数 t

        while (t-- > 0) {
            // 读取 n 和 m
            String[] nm = br.readLine().split(" ");
            int n = Integer.parseInt(nm[0]);
            int m = Integer.parseInt(nm[1]);

            long sum = 0; // 使用 long 防止溢出
            for (int i = 0; i < n; i++) {
                String[] row = br.readLine().split(" "); // 读取一行
                for (int j = 0; j < m; j++) {
                    sum += Long.parseLong(row[j]); // 累加每个元素
                }
            }
            out.println(sum); // 输出当前组的结果
        }
        out.flush();
    }
}

全部评论

相关推荐

牛客ID:561366855:期望薪资多少?难以相信这简历找不到工作。说明二本电子信息专业想对口就业非常难。
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务