namespace ZuHeShu
{
class Program
{
static void Main(string[] args)
{
Program p = new Program();
Console.WriteLine("******************************組合數(shù)***********************************");
Console.WriteLine("請任意輸入一個整數(shù):");
string num = Console.ReadLine();
try {
long.Parse(num);
List<string> als = new List<string>();
als = p.GetData(string.Empty, num.Trim(), als);
p.Display(als);
Console.WriteLine("\nY-Enter:繼續(xù)");
if (Console.ReadLine().Trim().ToLower() == "y")
{
Console.Clear();
Main(args);
}
}
catch {
Console.Clear();
Console.WriteLine("請輸入數(shù)字!");
Main(args);
}
}
private List<string> GetData(String strBase, String strSel, List<string> alRet)
{
if (strSel.Length == 1)
{
string temp=strBase + strSel;
if (!alRet.Contains(temp)&&!temp.StartsWith("0"))
{
alRet.Add(temp);
}
}
else
{
for (int i = 0; i < strSel.Length; i++)
{
GetData(strBase + strSel.Substring(i, 1), strSel.Remove(i, 1), alRet);
}
}
return alRet;
}
public void Display(List<string> dals)
{
int i = 1;
int a = dals.Count;
if (a == 0)
{
Console.WriteLine("該數(shù)沒有其他組合情況");
}
else
{
Console.WriteLine("不同的組合數(shù)共有" + a + "個,如下");
foreach (string number in dals)
{
Console.Write(number + "\t");
if (i++ % 5 == 0)
{
Console.WriteLine();
}
}
}
}
}
}
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com