我是靠谱客的博主 舒服红酒,最近开发中收集的这篇文章主要介绍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 乘法

最后

以上就是舒服红酒为你收集整理的kotlin 乘法_Kotlin程序将两个矩阵相乘的全部内容,希望文章能够帮你解决kotlin 乘法_Kotlin程序将两个矩阵相乘所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(40)

评论列表共有 0 条评论

立即
投稿
返回
顶部