感觉是不是规则还不够完善,如果你只是想要一个数组中相邻的两个元素不同的话,不考虑其它的顺序等等,那实现起来是比较容易的。先把这个数组排下序,升序降序无所谓,这样处理起来会简单很多,具体方法如下。/// <summary> /// 将有序的整形数组,进行相邻位都不相等的处理 /// </summary> /// <param name="intArr"></param> /// <returns></returns> public int[] ArrayHandle(int[] intArr) { int[] result = new int[intArr.Length]; int intMiddle = intArr.Length / 2; if (intArr[0] == intArr[intMiddle]) { throw new Exception("数组中有相同的元素过半,无法转换!"); } //将排过序的数组一次按上半部分元素,下半部分元素依次往新数组里面赋值 for (int i = 0, j = 0; i < intMiddle; i++) { result[j] = intArr[i]; j++; result[j] = intArr[intMiddle + 1]; } if (intArr.Length % 2 != 0) { result[result.Length - 1] = intArr[result.Length - 1]; } return result; }
用LINQ做的 string[] str = { "a", "a", "a", "c", "c", "b", "e", "e", "e", "e", "e", "g", "g", "f", "f", "f" }; var Query = str.Select((x, y) => new { x = x, y = y ,num=str.Where(t=>t==x).Select(t=>t).Count()}); foreach (var v in Query.OrderBy(x=>x.y%Query.Max(s=>s.num))) { Console.WriteLine(v.x); } /* a b e f a e g a e g c e f c e f */
string[] str = { "a", "a", "a", "c", "c", "b", "e", "e", "e", "e", "e", "g", "g", "f", "f", "f" }; var Query = str.Select((x, y) => new { x = x, y = y ,num=str.Where(t=>t==x).Select(t=>t).Count()}); foreach (var v in Query.OrderBy(x=>x.y%Query.Max(s=>s.num))) { Console.WriteLine(v.x+" "+v.y%Query.Max(s=>s.num)); } /* a 0 b 0 e 0 f 0 a 1 e 1 g 1 a 2 e 2 g 2 c 3 e 3 f 3 c 4 e 4 f 4 */ Console.ReadKey();
(不逆) 的Linq运用的太棒了。
to 不逆 。你看我来回帖!你才来的!那你要我来看撒呢!Linq吗?呵呵!
用快速排序 public class Sort { public Sort() { } private void Swap(ref int i, ref int j) //swap two integer { int t; t = i; i = j; j = t; } public void QiuckSort(int[] list, int low, int high) { if (high <= low) { //only one element in array list //so it do not need sort return; } else if (high == low + 1) { //means two elements in array list //so we just compare them if (list[low] > list[high]) { //exchange them Swap(ref list[low], ref list[high]); return; } } //more than 3 elements in the arrary list //begin QuickSort myQuickSort(list, low, high); } public void myQuickSort(int[] list, int low, int high) { if (low < high) { int pivot = Partition(list, low, high); myQuickSort(list, low, pivot - 1); myQuickSort(list, pivot + 1, high); } } private int Partition(int[] list, int low, int high) { //get the pivot of the arrary list int pivot; pivot = list[low]; while (low < high) { while (low < high && list[high] >= pivot) { high--; } if (low != high) { Swap(ref list[low], ref list[high]); low++; } while (low < high && list[low] <= pivot) { low++; } if (low != high) { Swap(ref list[low], ref list[high]); high--; } } return low; } } 就可以看出来了
/// <returns></returns> public int[] ArrayHandle(int[] intArr) { int[] result = new int[intArr.Length]; int intMiddle = intArr.Length / 2; if (intArr[0] == intArr[intMiddle]) { throw new Exception("数组中有相同的元素过半,无法转换!"); } //将排过序的数组一次按上半部分元素,下半部分元素依次往新数组里面赋值 for (int i = 0, j = 0; i < intMiddle; i++) { result[j] = intArr[i]; j++; result[j] = intArr[intMiddle + 1]; } if (intArr.Length % 2 != 0) { result[result.Length - 1] = intArr[result.Length - 1]; } return result; }
例如数组中元素如下:
a
a
a
c
c
b
e
e
e
e
e
g
g
f
f
f
如何重新生成数组,其中元素还是这些,只是改变其顺序,例如a和a不相邻……
数据结构我的水平更差
能想到的就是比对
例如数组A A[0]<==>A[1]比较,不同放入另一个数组
相同A[1]<===>A[2],不同A[2]直接放入另一个数组
相同A[2]<==>A[3]比较,不同,将A[3]放入另一个数组A[2]的位置
这样比较烦
呵呵
/// 将有序的整形数组,进行相邻位都不相等的处理
/// </summary>
/// <param name="intArr"></param>
/// <returns></returns>
public int[] ArrayHandle(int[] intArr)
{
int[] result = new int[intArr.Length];
int intMiddle = intArr.Length / 2;
if (intArr[0] == intArr[intMiddle])
{
throw new Exception("数组中有相同的元素过半,无法转换!");
}
//将排过序的数组一次按上半部分元素,下半部分元素依次往新数组里面赋值
for (int i = 0, j = 0; i < intMiddle; i++)
{
result[j] = intArr[i];
j++;
result[j] = intArr[intMiddle + 1];
}
if (intArr.Length % 2 != 0)
{
result[result.Length - 1] = intArr[result.Length - 1]; }
return result;
}
{
Console.WriteLine(v.x);
}
/*
a
b
e
f
a
e
g
a
e
g
c
e
f
c
e
f
*/
string[] str = { "a", "a", "a", "c", "c", "b", "e", "e", "e", "e", "e", "g", "g", "f", "f", "f" }; var Query = str.Select((x, y) => new { x = x, y = y ,num=str.Where(t=>t==x).Select(t=>t).Count()}); foreach (var v in Query.OrderBy(x=>x.y%Query.Max(s=>s.num)))
{
Console.WriteLine(v.x+" "+v.y%Query.Max(s=>s.num));
} /*
a 0
b 0
e 0
f 0
a 1
e 1
g 1
a 2
e 2
g 2
c 3
e 3
f 3
c 4
e 4
f 4 */ Console.ReadKey();
public class Sort
{
public Sort()
{ }
private void Swap(ref int i, ref int j)
//swap two integer
{
int t;
t = i;
i = j;
j = t;
} public void QiuckSort(int[] list, int low, int high)
{
if (high <= low)
{
//only one element in array list
//so it do not need sort
return;
}
else if (high == low + 1)
{
//means two elements in array list
//so we just compare them
if (list[low] > list[high])
{
//exchange them
Swap(ref list[low], ref list[high]);
return;
}
}
//more than 3 elements in the arrary list
//begin QuickSort
myQuickSort(list, low, high);
} public void myQuickSort(int[] list, int low, int high)
{
if (low < high)
{
int pivot = Partition(list, low, high);
myQuickSort(list, low, pivot - 1);
myQuickSort(list, pivot + 1, high);
}
} private int Partition(int[] list, int low, int high)
{
//get the pivot of the arrary list
int pivot;
pivot = list[low];
while (low < high)
{
while (low < high && list[high] >= pivot)
{
high--;
}
if (low != high)
{
Swap(ref list[low], ref list[high]);
low++;
}
while (low < high && list[low] <= pivot)
{
low++;
}
if (low != high)
{
Swap(ref list[low], ref list[high]);
high--;
}
}
return low;
}
}
就可以看出来了
public int[] ArrayHandle(int[] intArr)
{
int[] result = new int[intArr.Length];
int intMiddle = intArr.Length / 2;
if (intArr[0] == intArr[intMiddle])
{
throw new Exception("数组中有相同的元素过半,无法转换!");
}
//将排过序的数组一次按上半部分元素,下半部分元素依次往新数组里面赋值
for (int i = 0, j = 0; i < intMiddle; i++)
{
result[j] = intArr[i];
j++;
result[j] = intArr[intMiddle + 1];
}
if (intArr.Length % 2 != 0)
{
result[result.Length - 1] = intArr[result.Length - 1]; }
return result;
}