[英]How can I check to see if my array has an even permutation?
So I've created an array and shuffled it and I want to check if the array has an even permutation. 因此,我创建了一个数组并将其改组,我想检查该数组是否具有偶数排列。 Anyone have an idea as to how i can do that? 任何人都知道我该怎么做?
totalTiles = (column * row) ;
int[] randomNumberArray = new int[totalTiles];
for (int p = 0; p < totalTiles; p++)
{
randomNumberArray[p] = number;
number++;
}
Shuffle(randomNumberArray);
Method to shuffle array 随机排列数组的方法
static void Shuffle <T>(T[] array)
{
Random _random = new Random();
int n = array.Length;
for (int i = 0; i < n; i++)
{
int r = i + _random.Next(n - i);
T t = array[r];
array[r] = array[i];
array[i] = t;
}
}
Could this possibly help? 这可能有帮助吗? https://en.wikipedia.org/wiki/Heap%27s_algorithm generate the permutations then just check if % 2 == 0
then it's even? https://zh.wikipedia.org/wiki/Heap%27s_algorithm生成排列,然后仅检查% 2 == 0
那么它是否均匀? Just a guess. 只是一个猜测。
procedure generate(n : integer, A : array of any): if n = 1 then output(A) else for i := 0; i < n - 1; i += 1 do generate(n - 1, A) if n is even then swap(A[i], A[n-1]) else swap(A[0], A[n-1]) end if end for generate(n - 1, A) end if
Just quickly taken from the wiki page. 只需从Wiki页面快速获取。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.