c# - Rotating array with one fixed value -
i'm building strong logic , randomized keygen @ base 'partial key verification'. need add random function rotanting array sample found here @ stack overflow have different needs mine.
i'm starting array:
int[] q = new int[5]; (int = 0; < q.length; i++) { q[i] = i; }
now have:
q[0] = 0; q[1] = 1; q[2] = 2; q[3] = 3; q[4] = 4;
i need clockwise need maintain value immutable, example q[2] = 2;
then first step should result (by adding + 1 clockwise):
q[0] = 1; q[1] = 3; //bypassed 2nd value q[2] = 2; q[3] = 4; q[4] = 0; //come 0
a second step should (by adding again + 1 clockwise):
q[0] = 3; //bypassed 2nd value q[1] = 4; q[2] = 2; q[3] = 0; //come 0 q[4] = 1;
if possible need rollback function... lot
thanks both help: meanwhile created solution:
(int = 0; < q.length; i++) { if (i == fixed_int) { } //donothing else if ((q[i] + 1) == fixed_int) { q[i] = q[i] + 2; } else if ((q[i] + 1) == (q.length + 1)) { q[i] = 0; } else { q[i] = q[i] + 1; } }
this fail when fix value has not 2 more int forward it's not case. please don't use code, it's not working in situations. use accepted answer because rocks!
assuming array has @ least 2 elements, may try (set step
+1
, -1
different rotations):
void rotate(int[] a, int fix, int step) { int = (fix + step + a.length) % a.length; int n = (fix - step + a.length) % a.length; int t = a[i]; while(i != n) { int j = (i + step + a.length) % a.length; a[i] = a[j]; = j; } a[n] = t; }
Comments
Post a Comment