描述
相信大家都会做两个整数的 A+B 了,今天我们把它扩展到矩阵,如果给你两个 N*M 的矩阵,你会做么?
输入描述
第一行一个数 C(1<=C<=100), 表示测试数据的组数,对于每组测试数据 :
开始有两个数 N,M(1<=N<=1000,1<=M<=1000) 表示输入 NM 的矩阵,然后有 2N 行,每行有 M 个数,先后表示两个矩阵 A,B.
输出描述
对于每组测试数据,输出一个 N*M 的矩阵表示 A+B 的和 .
样例输入
2
3 4
1 2 3 4
2 3 4 5
2 3 4 6
5 6 7 8
2 3 4 5
6 5 4 2
2 2
1 2
3 4
4 3
2 1
样例输出
6 8 10 12
4 6 8 10
8 8 8 8
5 5
5 5
思路及代码
本来这题想用面向对象的方法来写的,练一下类。可是java的类超时了,C++的类数组又太大,没办法只能传统写法。
这里贴一下Java写得面向对象吧,应该没什么问题吧
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int t = input.nextInt();
while(t-- > 0) {
int n, m;
Matrix a = new Matrix();
Matrix b = new Matrix();
Matrix c = new Matrix();
n = input.nextInt();
m = input.nextInt();
a.setter(n, m);
b.setter(n, m);
c.setter(n, m);
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
a.num[i][j] = input.nextInt();
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
b.num[i][j] = input.nextInt();
}
}
c.add(a, b);
c.myPrint();
}
}
}
class Matrix {
int [][] num = new int [1002][1002];
int n, m;
void add(Matrix a, Matrix b) {
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
num[i][j] = a.num[i][j] + b.num[i][j];
}
}
}
void setter(int a, int b) {
n = a;
m = b;
}
void myPrint() {
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
System.out.printf("%d ", num[i][j]);
}
System.out.println();
}
}
}
C++的AC程序也贴一下
#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
#include <queue>
#include <vector>
#include <stack>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#define ll long long
#define maxn 1000
#define exp 0.0001
using namespace std;
int n, m;
int a[maxn][maxn], b[maxn][maxn];
int main() {
int t; scanf("%d", &t);
while(t--) {
scanf("%d %d", &n, &m);
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
scanf("%d", &a[i][j]);
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
scanf("%d", &b[i][j]);
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
a[i][j] += b[i][j];
printf("%d ", a[i][j]);
}
printf("
");
}
}
return 0;
}
后来我在java群里问,有个大佬给了我一个java输入优化,优化之后果然AC了,效率提高了一倍
这是Java的AC程序
import java.io.*;
import java.math.*;
import java.util.*;
import java.text.*;
public class Main {
public static void main(String[] args) {
FS input = new FS(System.in);
PrintWriter cout = new PrintWriter(System.out);
int t = input.nextInt();
while(t-- > 0) {
int n, m;
int [][] a = new int [1002][1002];
int [][] b = new int [1002][1002];
n = input.nextInt();
m = input.nextInt();
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
a[i][j] = input.nextInt();
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
b[i][j] = input.nextInt();
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
a[i][j] += b[i][j];
cout.print(a[i][j] + " ");
}
cout.println();
}
}
cout.close();
}
private static class FS {
BufferedReader br;
StringTokenizer st;
public FS(InputStream in) {
br = new BufferedReader(new InputStreamReader(in));
}
String next(){
while(st==null||!st.hasMoreElements()){
try{st = new StringTokenizer(br.readLine());}
catch(IOException e){e.printStackTrace();}
}
return st.nextToken();
}
int nextInt() {return Integer.parseInt(next());}
long nextLong() {return Long.parseLong(next());}
double nextDouble() { return Double.parseDouble(next());}
}
}