using System; using System.Collections.Generic; using System.Text;namespace Test { class Program { public static void Rotate(int[] arr, int bound) //旋转递归法,产生全排列 { int len = arr.Length; if (bound > len) { throw new Exception("Invalid Argument."); } if (bound < 2) { if (arr[2] != 4) //第三位不为4 { int sum = 0; int ori = 0; foreach (int i in arr) { sum += i; sum *= 10; if(ori==3&&i==5 ||ori==5&&i==3) //35相连 { sum = 0; break; } ori = i; } if (sum > 0) { Console.WriteLine(sum/10);//去掉最后的0 } } } else { for (int i = 0; i < bound; ++i) { Rotate(arr, bound - 1); //递归旋转剩下部分 int k = len - bound; int temp = arr[k]; for (; k < len; k++) //旋转数组 { if (k == len - 1) { arr[k] = temp; } else { arr[k] = arr[k + 1]; } } } } } public static void Main() { const int SIZE = 6; int[] arr = new int[SIZE] { 1, 2, 2, 3, 4, 5 }; Array.Sort(arr); Console.WriteLine("Begin..."); Rotate(arr, SIZE); } } }
#region //用循环实现 int[] num = { 1, 2, 2, 3, 4 }; int index1, index2, index3, index4; for (int i = 0; i < 5; i++) { for (int i1 = 1; i1 <= 4; i1++) { index1 = (i + i1)%5; for (int i2 = 1; i2 <= 3; i2++) { index2 =(index1 +i2)%5; //判断下标是否出现过 while (index2 == i || index2 == index1) { index2++; index2 %= 5; } index3 = index2; for (int i3 = 1; i3 <= 2; i3++) { //判断下标是否出现过 while (index3 == i ||index3 == index1 || index3 == index2) { index3++; index3 %= 5; } index4 = (index3 + 1)%5; //判断下标是否出现过 while (index4 == i || index4 == index1 || index4 == index2 || index4 == index3) { index4++; index4 %= 5; } Console.Write("{0} {1} {2} {3} {4}", i, index1,index2,index3,index4);
using System.Collections.Generic;
using System.Text;namespace Test
{
class Program
{
public static void Rotate(int[] arr, int bound) //旋转递归法,产生全排列
{
int len = arr.Length;
if (bound > len)
{
throw new Exception("Invalid Argument.");
}
if (bound < 2)
{
if (arr[2] != 4) //第三位不为4
{
int sum = 0;
int ori = 0;
foreach (int i in arr)
{
sum += i;
sum *= 10;
if(ori==3&&i==5 ||ori==5&&i==3) //35相连
{
sum = 0;
break;
}
ori = i;
}
if (sum > 0)
{
Console.WriteLine(sum/10);//去掉最后的0
} }
}
else
{
for (int i = 0; i < bound; ++i)
{
Rotate(arr, bound - 1); //递归旋转剩下部分
int k = len - bound;
int temp = arr[k];
for (; k < len; k++) //旋转数组
{
if (k == len - 1)
{
arr[k] = temp;
}
else
{
arr[k] = arr[k + 1];
}
}
}
}
}
public static void Main()
{
const int SIZE = 6;
int[] arr = new int[SIZE] { 1, 2, 2, 3, 4, 5 };
Array.Sort(arr);
Console.WriteLine("Begin...");
Rotate(arr, SIZE);
} }
}
//用循环实现
int[] num = { 1, 2, 2, 3, 4 };
int index1, index2, index3, index4;
for (int i = 0; i < 5; i++)
{
for (int i1 = 1; i1 <= 4; i1++)
{
index1 = (i + i1)%5;
for (int i2 = 1; i2 <= 3; i2++)
{
index2 =(index1 +i2)%5;
//判断下标是否出现过
while (index2 == i || index2 == index1)
{
index2++;
index2 %= 5;
}
index3 = index2;
for (int i3 = 1; i3 <= 2; i3++)
{
//判断下标是否出现过
while (index3 == i ||index3 == index1 || index3 == index2)
{
index3++;
index3 %= 5;
}
index4 = (index3 + 1)%5;
//判断下标是否出现过
while (index4 == i || index4 == index1
|| index4 == index2 || index4 == index3)
{
index4++;
index4 %= 5;
} Console.Write("{0} {1} {2} {3} {4}", i, index1,index2,index3,index4);
Console.WriteLine("\t{0} {1} {2} {3} {4}"
,num[i], num[index1],num[index2],num[index3],num[index4]);
}
}
}
}
#endregion
如果数组大小是100个呢?
写100个嵌套循环?呵呵
不过shrinerain(圣影雨)的算法未排除重复数据.......
如果数组大小不确定,当然要选择递归。这毫无疑问!
//用循环实现
int[] num = { 1, 2, 2, 3, 4 };
int index1, index2, index3, index4;
for (int i = 0; i < 5; i++)
{
for (int i1 = 1; i1 <= 4; i1++)
{
index1 = (i + i1) % 5;
for (int i2 = 1; i2 <= 3; i2++)
{
index2 = (index1 + i2) % 5;
//判断下标是否出现过
while (index2 == i || index2 == index1)
{
index2++;
index2 %= 5;
}
index3 = index2;
for (int i3 = 1; i3 <= 2; i3++)
{
//判断下标是否出现过
while (index3 == i || index3 == index1 || index3 == index2)
{
index3++;
index3 %= 5;
}
index4 = (index3 + 1) % 5;
//判断下标是否出现过
while (index4 == i || index4 == index1
|| index4 == index2 || index4 == index3)
{
index4++;
index4 %= 5;
} Console.WriteLine("{0} {1} {2} {3} {4}\t{5} {6} {7} {8} {9}", i, index1, index2, index3, index4
, num[i], num[index1], num[index2], num[index3], num[index4]);
index3 = (index3 + 1) % 5;
}
index2 = (index2 + 1) % 5;
}
index1 = (index1 + 1) % 5;
}
}
#endregion
循环结束时忘记累加了。