L1-8 矩阵A乘以B (15分)

给定两个矩阵AAABBB,要求你计算它们的乘积矩阵ABABAB。需要注意的是,只有规模匹配的矩阵才可以相乘。即若AAARaR_aRa行、CaC_aCa列,BBBRbR_bRb行、CbC_bCb列,则只有CaC_aCaRbR_bRb相等时,两个矩阵才能相乘。

输入格式:

输入先后给出两个矩阵AAABBB。对于每个矩阵,首先在一行中给出其行数RRR和列数CCC,随后RRR行,每行给出CCC个整数,以1个空格分隔,且行首尾没有多余的空格。输入保证两个矩阵的RRRCCC都是正数,并且所有整数的绝对值不超过100。

输出格式:

若输入的两个矩阵的规模是匹配的,则按照输入的格式输出乘积矩阵ABABAB,否则输出Error: Ca != Rb,其中CaAAA的列数,RbBBB的行数。

输入样例1:

2 3
1 2 3
4 5 6
3 4
7 8 9 0
-1 -2 -3 -4
5 6 7 8

    
    

输出样例1:

2 4
20 22 24 16
53 58 63 28

    
    

输入样例2:

3 2
38 26
43 -5
0 17
3 2
-11 57
99 68
81 72

    
    

输出样例2:

Error: 2 != 3

    
    

最后测试点一个超时

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.StringTokenizer;

//** Class for buffered reading int and double values *//*
class Reader {
	static BufferedReader reader;
	static StringTokenizer tokenizer;

	// ** call this method to initialize reader for InputStream *//*
	static void init(InputStream input) {
		reader = new BufferedReader(new InputStreamReader(input));
		tokenizer = new StringTokenizer("");
	}

	// ** get next word *//*
	static String next() throws IOException {
		while (!tokenizer.hasMoreTokens()) {
			// TODO add check for eof if necessary
			tokenizer = new StringTokenizer(reader.readLine());
		}
		return tokenizer.nextToken();
	}
	static boolean hasNext()throws IOException {
		return tokenizer.hasMoreTokens();
	}
	static String nextLine() throws IOException{
		return reader.readLine();
	}
	static char nextChar() throws IOException{
		return next().charAt(0);
	}
	static int nextInt() throws IOException {
		return Integer.parseInt(next());
	}

	static float nextFloat() throws IOException {
		return Float.parseFloat(next());
	}
}
public class Main {

	public static void main(String[] args) throws IOException {
		Reader.init(System.in);

		int Ra = Reader.nextInt();
		int Ca = Reader.nextInt();
		int[][] A = new int[Ra][Ca];

		// input matrixA
		for (int i = 0; i < Ra; i++) {
			for (int j = 0; j < Ca; j++) {
				A[i][j] = Reader.nextInt();
			}
		}

		int Rb = Reader.nextInt();

		if (Ca == Rb) {
			int Cb = Reader.nextInt();
			int[][] B = new int[Rb][Cb];

			// input matrixB
			for (int i = 0; i < Rb; i++) {
				for (int j = 0; j < Cb; j++) {
					B[i][j] = Reader.nextInt();
				}
			}

			System.out.println(Ra + " " + Cb);
			int[][] C = new int[Ra][Cb];

			for (int i = 0; i < Ra; i++) {
				for (int j = 0; j < Cb; j++) {
					for (int k = 0; k < Ca; k++) {
						C[i][j] += A[i][k] * B[k][j];
					}
					System.out.print(C[i][j]);

					if (j != Cb-1) {
						System.out.print(" ");
					}
				}
				System.out.println();
			}

		} else {
			System.out.println("Error: " + Ca + " != " + Rb);
		}
	}
}

将io操作拎出来放进静态方法里就不会超时了

import java.io.IOException;
import java.io.*;
import java.util.*;

//** Class for buffered reading int and double values *//*
class Reader {
	static BufferedReader reader;
	static StringTokenizer tokenizer;

	// ** call this method to initialize reader for InputStream *//*
	static void init(InputStream input) {
		reader = new BufferedReader(new InputStreamReader(input));
		tokenizer = new StringTokenizer("");
	}

	// ** get next word *//*
	static String next() throws IOException {
		while (!tokenizer.hasMoreTokens()) {
			// TODO add check for eof if necessary
			tokenizer = new StringTokenizer(reader.readLine());
		}
		return tokenizer.nextToken();
	}

	static String nextLine() throws IOException {
		return reader.readLine();
	}

	static int nextInt() throws IOException {
		return Integer.parseInt(next());
	}

	static long nextLong() throws IOException {
		return Long.parseLong(next());
	}

	static double nextDouble() throws IOException {
		return Double.parseDouble(next());
	}
}

public class Main {
	public static void main(String[] args) throws IOException {
		Reader.init(System.in);

		int [][] metrix1 = get_metrix();
		int [][] metrix2 = get_metrix();
		
		if (metrix1[0].length!=metrix2.length) {
			System.out.println("Error: "+metrix1[0].length+" != "+metrix2.length);
		}else {
			System.out.println(metrix1.length+" "+metrix2[0].length);
			for (int i = 0; i < metrix1.length; i++) {
				for (int j = 0; j < metrix2[0].length; j++) {
					int sum = 0;
					for (int k = 0; k < metrix1[0].length; k++) {
						sum+=metrix1[i][k]*metrix2[k][j];
					}				
					if (j==metrix2[0].length-1) {
						System.out.println(sum);
					}else {
						System.out.print(sum+" ");
					}
				}
				
			}
		}	
	}
	static int[][]get_metrix() throws IOException{
		int r1 = Reader.nextInt();
		int c1 = Reader.nextInt();
		int [][] metrix1 = new int[r1][c1];
		for (int i = 0; i < metrix1.length; i++) {
			for (int j = 0; j < metrix1[i].length; j++) {
				metrix1[i][j] = Reader.nextInt();
			}
		}
		return metrix1;
	}
}

发布了45 篇原创文章 · 获赞 8 · 访问量 1766

猜你喜欢

转载自blog.csdn.net/weixin_43888039/article/details/104109855