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



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

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

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

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][n];
		int[][] c=new int[m][n];
		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() );
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值