您的位置:首页 > Web前端

Java实现ALGO-86 矩阵乘法 (算法训练) (ACM中Java的快速输入:BufferedReader+StringTokenizer)

2019-03-16 12:25 288 查看

Java各种输入方法参考链接:

https://www.cpe.ku.ac.th/~jim/java-io.html

本文采用BufferedReader+StringTokenizer快速输入方法

[code]import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

public static void main(String[] args) throws IOException  {
//		Scanner in=new Scanner(System.in);
Reader.init( System.in ); // connect Reader to an input stream
//		int m=in.nextInt();
//		int s=in.nextInt();
//		int n=in.nextInt();
int m=Reader.nextInt();
int s=Reader.nextInt();
int n=Reader.nextInt();
int[][] a=new int[m][s];
int[][] b=new int[s]
;
int[][] c=new int[m]
;
for(int i=0;i<m;i++) {
for(int j=0;j<s;j++) {
a[i][j]=Reader.nextInt();
}
}
for(int i=0;i<s;i++) {
for(int j=0;j<n;j++) {
b[i][j]=Reader.nextInt();
}
}
for(int i=0;i<m;i++) {
for(int k=0;k<n;k++) {
for(int j=0;j<s;j++) {
c[i][k]+=a[i][j]*b[j][k];
}
if(k==0)
System.out.print(c[i][0]);
else
System.out.print(" "+c[i][k]);
}
System.out.println();
}
}

}

/** 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 int nextInt() throws IOException {
return Integer.parseInt( next() );
}

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

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: