fun main(args: Array<String>) {
val arrayA: Array<IntArray> = arrayOf(
intArrayOf(5, 5, 50, 5),
intArrayOf(5, 5, 5, 5),
intArrayOf(5, 5, 5, 2)
)
val arrayB: Array<IntArray> = arrayOf(
intArrayOf(5, 5),
intArrayOf(5, 1),
intArrayOf(15, 5),
intArrayOf(5, 1)
)
println("Array A:")
print(arrayA)
println()
println("Array B:")
print(arrayB)
println()
println("Array A, first min value position:")
val (yMinA, xMinA) = findFirstMinValuePosition(arrayA)
print(xMinA, yMinA)
println()
println("Array B, first min value position:")
val (yMinB, xMinB) = findFirstMinValuePosition(arrayB)
print(xMinB, yMinB)
println()
println("Array A, first max value position:")
val (yMaxA, xMaxA) = findFirstMaxValuePosition(arrayA)
print(xMaxA, yMaxA)
println()
println("Array B, first max value position:")
val (yMaxB, xMaxB) = findFirstMaxValuePosition(arrayB)
print(xMaxB, yMaxB)
swap(arrayA, arrayB, yMinA, xMinA, yMinB, xMinB)
swap(arrayA, arrayB, yMaxA, xMaxA, yMaxB, xMaxB)
println("----")
println()
println("Array A after two swaps:")
print(arrayA)
println()
println("Array B after two swaps:")
print(arrayB)
}
fun findFirstMinValuePosition(array: Array<IntArray>): Pair<Int, Int> {
val y: Int = array.size
val x: Int = if (y > 0) array[0].size else 0
if (y == 0) throw IllegalArgumentException(
"The dimension of the array along the Y axis is zero.")
if (x == 0) throw IllegalArgumentException(
"The dimension of the array along the Y axis is zero.")
var yResult = 0
var xResult = 0
var minValue: Int = array[0][0]
for (yIndex in array.indices) {
for (xIndex in array[yIndex].indices) {
if (minValue > array[yIndex][xIndex])
{
yResult = yIndex
xResult = xIndex
minValue = array[yIndex][xIndex]
}
}
}
return Pair(yResult, xResult)
}
fun findFirstMaxValuePosition(array: Array<IntArray>): Pair<Int, Int> {
val y: Int = array.size
val x: Int = if (y > 0) array[0].size else 0
if (y == 0) throw IllegalArgumentException(
"The dimension of the array along the Y axis is zero.")
if (x == 0) throw IllegalArgumentException(
"The dimension of the array along the Y axis is zero.")
var yResult = 0
var xResult = 0
var maxValue: Int = array[0][0]
for (yIndex in array.indices) {
for (xIndex in array[yIndex].indices) {
if (maxValue < array[yIndex][xIndex])
{
yResult = yIndex
xResult = xIndex
maxValue = array[yIndex][xIndex]
}
}
}
return Pair(yResult, xResult)
}
fun swap(arrayA: Array<IntArray>, arrayB: Array<IntArray>, yA: Int, xA: Int, yB: Int, xB: Int) {
val tempB: Int = arrayB[yB][xB]
arrayB[yB][xB] = arrayA[yA][xA]
arrayA[yA][xA] = tempB
}
fun print(array: Array<IntArray>) {
for (row: IntArray in array) {
println(row.contentToString())
}
}
fun print(x: Int, y: Int) {
println("x: $x, y: $y")
}
// --
private static (int y, int x) FindFirstMaxValuePosition(int[,] array)
{
if (array is null) throw new ArgumentNullException(nameof(array));
int y = array.GetLength(0);
int x = array.GetLength(1);
if (y == 0) throw new InvalidOperationException(
"The dimension of the array along the Y axis is zero.");
if (x == 0) throw new InvalidOperationException(
"The dimension of the array along the X axis is zero.");
int yResult = 0;
int xResult = 0;
int maxValue = array[0, 0];
for (int yIndex = 0; yIndex < y; yIndex++)
{
for (int xIndex = 0; xIndex < x; xIndex++)
{
if (maxValue < array[yIndex, xIndex])
{
yResult = yIndex;
xResult = xIndex;
maxValue = array[yIndex, xIndex];
}
}
}
return (yResult, xResult);
}
enum class Константы {
НОМЕР_ОДИН,
НОМЕР_ДВА
}
// ---
// Константы.НОМЕР_ОДИН != Константы.НОМЕР_ДВА
enum class Константа {
НОМЕР_ОДИН,
НОМЕР_ДВА
}
// ---
// Константа.НОМЕР_ОДИН != Константа.НОМЕР_ДВА