数字排列组合
//不重复排列组合N!
public class Program
{
static List<string> sorts = new List<string>();
static void Main(string[] args)
{
GetSortNums(4, "");
for(int i = 0; i < sorts.Count; i++)
{
Console.Write(sorts[i] + "\t");
if ((i+1) % 4==0)
{
Console.WriteLine();
}
}
Console.ReadKey();
}
public static void GetSortNums(int length, string combins)
{
for (int a = 0; a < length; a++)
{
string newcombins = combins;
if (!newcombins.Contains(a.ToString()))
{
newcombins += a;
if (newcombins.Length == length)
{
if (!sorts.Contains(newcombins))
{
sorts.Add(newcombins);
}
}
else
{
GetSortNums(length, newcombins);
}
}
}
}
}
//重复排列组合N^P
public static void GetSortSigns(int length, int depth, string combins)
{
for (int a = 0; a < length; a++)
{
string newcombins = combins;
newcombins += a;
if (newcombins.Length == depth)
{
if (!sorts.Contains(newcombins))
{
sorts.Add(newcombins);
}
}
else
{
GetSortSigns(length, depth, newcombins);
}
}
}

浙公网安备 33010602011771号