kotlin 乘法_Kotlin程序将两个矩阵相乘

kotlin 乘法

Given two matrices, we have to multiply them.

给定两个矩阵,我们必须将它们相乘。

Note: For matrix multiplication, the number of columns in the first matrix must be equal to the number of rows in the second matrix.

注意:对于矩阵乘法,第一个矩阵中的列数必须等于第二个矩阵中的行数。

Example:

例:

    Input:
    matrix 1:
    [3, 4, 5, 6]
    [7, 8, 9, 8]
    [3, 2, 1, 2]

    matrix 2:
    [3, 4]
    [2, 3]
    [4, 5]
    [6, 7]

    Output:
    [73, 91]
    [121, 153]
    [29, 37]

在Kotlin中将两个矩阵相乘的程序 (Program to multiply two matrices in Kotlin)

package com.includehelp

import java.util.*

// Main function, Entry Point of Program
fun main(args: Array<String>) {
    //Input Stream
    val scanner = Scanner(System.`in`)

    //Input no of rows and column
    print("Enter the number of rows and columns of First matrix : ")
    val rows1   = scanner.nextInt()
    val column1 = scanner.nextInt()

    //Input no of rows and column
    print("Enter the number of rows and columns of Second matrix : ")
    val rows2   = scanner.nextInt()
    val column2 = scanner.nextInt()

    //No of Col in first Matrix must be Equal to no of rows in second Matrix
    if(column1 != rows2){
        println("No of Column in first Matrix must be Equal to no of rows in second Matrix !!")
        return
    }

    //Create First Matrix Array
    val matrixA     = Array(rows1) { IntArray(column1) }

    //Create Second Matrix Array
    val matrixB     = Array(rows2) { IntArray(column2) }

    // create Product Matrix Array
    val productMatrix   = Array(rows1) { IntArray(column2) }


    //Input Matrix
    println("Enter the Elements of First Matrix ($rows1 X $column1} ): ")
    for(i in matrixA.indices){
        for(j in matrixA[i].indices){
            print("matrixA[$i][$j]: ")
            matrixA[i][j]=scanner.nextInt()
        }
    }

    //Input Matrix
    println("Enter the Elements of Second Matrix ($rows2 X $column2} ): ")
    for(i in matrixB.indices){
        for(j in matrixB[i].indices){
            print("matrixB[$i][$j]: ")
            matrixB[i][j]=scanner.nextInt()
        }
    }

    //print Matrix A
    println("Matrix A : ")
    for(i in matrixA.indices){
        println("${matrixA[i].contentToString()} ")
    }

    //print Matrix B
    println("Matrix B : ")
    for(i in matrixB.indices){
        println("${matrixB[i].contentToString()} ")
    }

    //Product of Matrices
    for(i in productMatrix.indices){
        for(j in productMatrix[i].indices){
            for(k in matrixB.indices)
                productMatrix[i][j]=productMatrix[i][j] + (matrixA[i][k] * matrixB[k][j])
        }
    }

    //Print Product of Matrices
    println("Product of the Matrices:")
    for(i in productMatrix.indices){
        println("${productMatrix[i].contentToString()} ")
    }
}

Output

输出量

Run 1:
Enter the number of rows and columns of First matrix : 3
4
Enter the number of rows and columns of Second matrix : 3
3
No of Column in first Matrix must be Equal to no of rows in second Matrix !!
---------------
Run 2:
Enter the number of rows and columns of First matrix : 3
4
Enter the number of rows and columns of Second matrix : 4
2
Enter the Elements of First Matrix (3 X 4} ):
matrixA[0][0]: 3
matrixA[0][1]: 4
matrixA[0][2]: 5
matrixA[0][3]: 6
matrixA[1][0]: 7
matrixA[1][1]: 8
matrixA[1][2]: 9
matrixA[1][3]: 8
matrixA[2][0]: 3
matrixA[2][1]: 2
matrixA[2][2]: 1
matrixA[2][3]: 2
Enter the Elements of Second Matrix (4 X 2} ):
matrixB[0][0]: 3
matrixB[0][1]: 4
matrixB[1][0]: 2
matrixB[1][1]: 3
matrixB[2][0]: 4
matrixB[2][1]: 5
matrixB[3][0]: 6
matrixB[3][1]: 7
Matrix A :
[3, 4, 5, 6]
[7, 8, 9, 8]
[3, 2, 1, 2]
Matrix B :
[3, 4]
[2, 3]
[4, 5]
[6, 7]
Product of the Matrices:
[73, 91]
[121, 153]
[29, 37]
----
Run 3:
Enter the number of rows and columns of First matrix : 2
3
Enter the number of rows and columns of Second matrix : 3
2
Enter the Elements of First Matrix (2 X 3} ):
matrixA[0][0]: 3
matrixA[0][1]: 4
matrixA[0][2]: 5
matrixA[1][0]: 6
matrixA[1][1]: 3
matrixA[1][2]: 2
Enter the Elements of Second Matrix (3 X 2} ):
matrixB[0][0]: 1
matrixB[0][1]: 2
matrixB[1][0]: 2
matrixB[1][1]: 3
matrixB[2][0]: 2
matrixB[2][1]: 1
Matrix A :
[3, 4, 5]
[6, 3, 2]
Matrix B :
[1, 2]
[2, 3]
[2, 1]
Product of the Matrices:
[21, 23]
[16, 23]


翻译自: https://www.includehelp.com/kotlin/multiply-two-matrices.aspx

kotlin 乘法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值