C#练习题答案: 计算在数组矩阵反转数【难度:2级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

mac2024-03-06  24

计算在数组矩阵反转数【难度:2级】:

答案1:

public class Kata { public static int CountInversions(int[] a) { int cnt = 0; for (int i = 0; i < a.Length - 1; i++) for (int j = i + 1; j < a.Length; j++) if (a[i] > a[j]) cnt++; return cnt; } }

答案2:

using System.Linq; public class Kata { public static int CountInversions(int[] array) => array.Select((a, i) => array.Skip(i).Count(b => a > b)).Sum(); }

答案3:

public class Kata { public static int CountInversions(int[] array) { int count=0; for (int i=0;i<array.Length-1; i++) for(int j=0; j< array.Length-1-i;j++) if ( array[j] > array[j+1]) { int temp = array[j+1]; array[j+1] = array[j]; array[j] = temp ; count++; } return count; } }

答案4:

public class Kata { public static int CountInversions(int[] array) { var count = 0; for (int write = 0; write < array.Length; write++) { for (int sort = 0; sort < array.Length - 1; sort++) { if (array[sort] > array[sort + 1]) { count++; var temp = array[sort + 1]; array[sort + 1] = array[sort]; array[sort] = temp; } } } return count; } }

答案5:

using System; public class Kata { public static int CountInversions(int[] array) { int count = 0; for (int i = 0; i < array.Length; i++) { for (int j = i + 1; j < array.Length; j++) { if (array[i] > array[j]) count++; } } return count; } }

答案6:

using System; public class Kata { public static int CountInversions(int[] array) { // Perform a bubble sort and count the number of swaps var swaps = 0; for (var i = 0; i < array.Length - 1; i++) { for (var j = 0; j < array.Length - 1 - i; j++) { if (array[j] > array[j+1]) { var temp = array[j+1]; array[j+1] = array[j]; array[j] = temp; swaps++; } } } return swaps; } }

答案7:

using System.Collections.Generic; public class Kata { public static int CountInversions(int[] array) { int inversions = 0; int arrayLength = array.Length; for (int index=0; index<arrayLength; index++) { // test all grid cells for pairs with higher index cells int currentValue = array[index]; for (int x=index+1; x<arrayLength; x++) { // find all pairs with higher index than current selected cell int pairValue = array[x]; // get the next highest index cell if (currentValue > pairValue) { // add inversion if paired cell value is less than current cell value inversions += 1; } } } return inversions; } }

答案8:

public class Kata { public static int CountInversions(int[] array) { int count = 0; for (int i = 0; i < array.Length-1; i++) { for (int j = 0; j < array.Length-i-1; j++) { if (array[j]>array[j+1]) { int hold = array[j]; array[j] = array[j+1]; array[j+1] = hold; count++; } } } return count; } }

答案9:

using System; using System.Linq; public class Kata { public static int CountInversions(int[] a) { int[] tempa = a.OrderBy(x => x).ToArray(); int res = 0; while (!tempa.SequenceEqual(a)) { for (int i = 0; i < a.Length - 1; i++) { if(a[i] > a[i+1]) { int check = a[i + 1]; a[i + 1] = a[i]; a[i] = check; res++; } } } return res; } }

答案10:

public class Kata { public static int CountInversions(int[] array) { int count = 0; for (int i = 0; i < array.Length; i++) { for (int j = i; j < array.Length; j++) { if (array[i] > array[j]) count++; } } return count; } }
最新回复(0)