asp.net求3位不同數位組合數

來源:互聯網
上載者:User

簡單的: 複製代碼 代碼如下:public partial class _Default : System.Web.UI.Page
{
string m1 = "";
protected void Page_Load(object sender, EventArgs e)
{
string n = "123";
string m = "";
zuhe(n);
for (int j = 2; j > -1; j--)
{
m = m + n[j];
}
zuhe(m);
}
private void zuhe(string ssel)
{
for (int i = 0; i < ssel.Length; i++)
{
m1 = ssel + ssel.Substring(i, 1);
Response.Write(m1.Remove(i,1) + "<br>");
}
}
}

CSDN的(xiaoshen1127 ):WINFORM版 複製代碼 代碼如下:using System;
using System.Collections.Generic;

namespace ZuHeShu
{
class Program
{

static void Main(string[] args)
{

Program p = new Program();
Console.WriteLine("******************************組合數***********************************");
Console.WriteLine("請任意輸入一個整數:");
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:繼續");
if (Console.ReadLine().Trim().ToLower() == "y")
{
Console.Clear();
Main(args);
}
}
catch {
Console.Clear();
Console.WriteLine("請輸入數字!");
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("該數沒有其他組合情況");
}
else
{
Console.WriteLine("不同的組合數共有" + a + "個,如下");
foreach (string number in dals)
{
Console.Write(number + "\t");
if (i++ % 5 == 0)
{
Console.WriteLine();
}
}
}
}
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.