日期:2014-05-20  浏览次数:20916 次

一算法
求教各位,如果用任意的五个数字(比如:1,2,3,4,5)生成可能生成的所有三位数,要实现这样的算法,该怎么写呀?!

------解决方案--------------------
非常笨的办法
int[] intArray = new int[] { 1, 2, 3, 4, 5 };
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
if (i == j)
continue;
for (int k = 0; k < 5; k++)
{
if (i == k || j == k)
continue;
Console.WriteLine(intArray[i] * 100 + intArray[j] * 10 +intArray[k]);

}
}
}
Console.ReadKey();
------解决方案--------------------
int[] num = new int[] { 1, 2, 3, 4, 5 };

for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
for (int k = 0; k < 5; k++)
Console.WriteLine(num[i].ToString() + num[j].ToString() + num[k].ToString());
------解决方案--------------------
//才发现后面的参数时多余的
public void Combination(string ANumbers, string APath, int ALen)
{
if (APath.Length > = ALen)
{
richTextBox1.AppendText(APath + "\r\n ");
return;
}
for (int i = 0; i < ANumbers.Length; i++)
{
string vNumbers = ANumbers.Remove(i, 1);
Combination(vNumbers, APath + ANumbers.Substring(i, 1), ALen);
}
return;
}
private void button3_Click(object sender, EventArgs e)
{
Combination( "12345 ", " ", 3);
}