kotlin 第一个程序_Kotlin程序减去两个矩阵
生活随笔
收集整理的這篇文章主要介紹了
kotlin 第一个程序_Kotlin程序减去两个矩阵
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
kotlin 第一個程序
Given two matrices, we have to subtract them.
給定兩個矩陣,我們必須將它們相減。
Example:
例:
Input:matrix 1:[2, 3, 5][0, 5, 4][2, 1, 2]matrix 2:[6, 34, 2][5, 7, 5][3, 4, 3]Output:[-4, -31, 3][-5, -2, -1][-1, -3, -1]在Kotlin中減去兩個矩陣的程序 (Program to subtract two matrices in Kotlin)
package com.includehelpimport java.util.*// Main function, Entry Point of Program fun main(args: Array<String>) {//variable of rows and colval rows: Intval column: Int//Input Streamval scanner = Scanner(System.`in`)//Input no of rows and columnprint("Enter the number of rows and columns of matrix : ")rows = scanner.nextInt()column = scanner.nextInt()//Create Arrayval matrixA = Array(rows) { IntArray(column) }val matrixB = Array(rows) { IntArray(column) }val matrixSum = Array(rows) { IntArray(column) }//Input Matrixprintln("Enter the Elements of First Matrix ($rows X $column} ): ")for(i in matrixA.indices){for(j in matrixA[i].indices){print("matrixA[$i][$j]: ")matrixA[i][j]=scanner.nextInt()}}//Input Matrixprintln("Enter the Elements of Second Matrix ($rows X $column} ): ")for(i in matrixB.indices){for(j in matrixB[i].indices){print("matrixB[$i][$j]: ")matrixB[i][j]=scanner.nextInt()}}//print Matrix Aprintln("Matrix A : ")for(i in matrixA.indices){println("${matrixA[i].contentToString()} ")}//print Matrix Bprintln("Matrix B : ")for(i in matrixB.indices){println("${matrixB[i].contentToString()} ")}//Perform Subtractionfor(i in matrixSum.indices){for(j in matrixSum[i].indices){matrixSum[i][j] = matrixA[i][j] - matrixB[i][j]}}//Print Sum of Matricesprintln("Sum of the Matrices:")for(i in matrixSum.indices){println("${matrixSum[i].contentToString()} ")} }Output
輸出量
Run 1: Enter the number of rows and columns of matrix : 3 3 Enter the Elements of First Matrix (3 X 3} ): matrixA[0][0]: 2 matrixA[0][1]: 3 matrixA[0][2]: 5 matrixA[1][0]: 0 matrixA[1][1]: 5 matrixA[1][2]: 4 matrixA[2][0]: 2 matrixA[2][1]: 1 matrixA[2][2]: 2 Enter the Elements of Second Matrix (3 X 3} ): matrixB[0][0]: 6 matrixB[0][1]: 34 matrixB[0][2]: 2 matrixB[1][0]: 5 matrixB[1][1]: 7 matrixB[1][2]: 5 matrixB[2][0]: 3 matrixB[2][1]: 4 matrixB[2][2]: 3 Matrix A : [2, 3, 5] [0, 5, 4] [2, 1, 2] Matrix B : [6, 34, 2] [5, 7, 5] [3, 4, 3] Sum of the Matrices: [-4, -31, 3] [-5, -2, -1] [-1, -3, -1]翻譯自: https://www.includehelp.com/kotlin/subtract-two-matrices.aspx
kotlin 第一個程序
總結
以上是生活随笔為你收集整理的kotlin 第一个程序_Kotlin程序减去两个矩阵的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c# 情感倾向_C否则-能力倾向问题与解
- 下一篇: cd-rom门锁定什么意思_CD-ROM