using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { List<string> list = new List<string>(); list.AddRange(GetString(new string[] {"A","B","CD","F" })); list.AddRange(GetString(new string[] { "A", "B", "DC", "F" })); foreach (string s in list) { Console.WriteLine(string.Format("{0}{1}E{2}{3}{4}", s[0], s[1], s[2], s[3], s[4])); } Console.Read(); } static string[] GetString(string[] cs) { if (cs.Length == 2) { return new string[] { cs[0] + cs[1], cs[1] + cs[0] }; } List<string> list = new List<string>(); for (int i = 0; i < cs.Length-1; i++) { List<string> tmp=new List<string>(cs); tmp.RemoveAt(i); string[] sub=GetString(tmp.ToArray()); List<string> tmp2 = new List<string>(); foreach (string s in sub) { tmp2.Add(cs[i] + s); } list.AddRange(tmp2.ToArray()); } return list.ToArray(); } } }
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls;public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string test = "A B C D E F"; string result = test.Substring(0, 5) + test.Substring(6, 5); Response.Write(result); } }输出是A B CD E F 按你的意思把C D连到一起,那E也到不了第三位呀.. 我没有太看清你的意思,这是我理解的,你看对不对.
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<string> list = new List<string>();
list.AddRange(GetString(new string[] {"A","B","CD","F" }));
list.AddRange(GetString(new string[] { "A", "B", "DC", "F" }));
foreach (string s in list)
{
Console.WriteLine(string.Format("{0}{1}E{2}{3}{4}", s[0], s[1], s[2], s[3], s[4]));
}
Console.Read();
}
static string[] GetString(string[] cs)
{
if (cs.Length == 2) {
return new string[] { cs[0] + cs[1], cs[1] + cs[0] };
}
List<string> list = new List<string>();
for (int i = 0; i < cs.Length-1; i++)
{
List<string> tmp=new List<string>(cs);
tmp.RemoveAt(i);
string[] sub=GetString(tmp.ToArray());
List<string> tmp2 = new List<string>();
foreach (string s in sub)
{
tmp2.Add(cs[i] + s);
}
list.AddRange(tmp2.ToArray());
}
return list.ToArray();
}
}
}
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string test = "A B C D E F";
string result = test.Substring(0, 5) + test.Substring(6, 5);
Response.Write(result);
}
}输出是A B CD E F 按你的意思把C D连到一起,那E也到不了第三位呀..
我没有太看清你的意思,这是我理解的,你看对不对.
例如:C D E A B F 或者 A F E D C B 等等
ABECDF
ABEFCD
ACEDBF
ACEDFB
BAECDF
BAEFCD
BCEDAF
BCEDFA
CDEABF
CDEAFB
CDEBAF
CDEBFA
ABEDCF
ABEFDC
ADECBF
ADECFB
BAEDCF
BAEFDC
BDECAF
BDECFA
DCEABF
DCEAFB
DCEBAF
DCEBFA
——————————————————————————————
人家要的是结果不分开~~
大哥看题!要求 C D 连在一起,,E 一定要在第三位
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<string> list = new List<string>();
list.AddRange(GetString(new string[] {"A","B","CD","F" }));
list.AddRange(GetString(new string[] { "A", "B", "DC", "F" }));
foreach (string s in list)
{
string cd = s[1].ToString() + s[2].ToString();
if (cd == "CD" || cd == "DC") continue;
Console.WriteLine(string.Format("{0}{1}E{2}{3}{4}", s[0], s[1], s[2], s[3], s[4]));
}
Console.Read();
}
static string[] GetString(string[] cs)
{
if (cs.Length == 2) {
return new string[] { cs[0] + cs[1], cs[1] + cs[0] };
}
List<string> list = new List<string>();
for (int i = 0; i < cs.Length-1; i++)
{
List<string> tmp=new List<string>(cs);
tmp.RemoveAt(i);
string[] sub=GetString(tmp.ToArray());
List<string> tmp2 = new List<string>();
foreach (string s in sub)
{
tmp2.Add(cs[i] + s);
}
list.AddRange(tmp2.ToArray());
}
return list.ToArray();
}
}
}
ABECDF
ABEFCD
BAECDF
BAEFCD
CDEABF
CDEAFB
CDEBAF
CDEBFA
ABEDCF
ABEFDC
BAEDCF
BAEFDC
DCEABF
DCEAFB
DCEBAF
DCEBFA
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<string> list = new List<string>();
list.AddRange(GetString(new string[] {"A","B","CD","F" }));
list.AddRange(GetString(new string[] { "A", "B", "DC", "F" }));
foreach (string s in list)
{
string cd = s[1].ToString() + s[2].ToString();
if (cd == "CD" || cd == "DC") continue;
Console.WriteLine(string.Format("{0}{1}E{2}{3}{4}", s[0], s[1], s[2], s[3], s[4]));
}
Console.Read();
}
static string[] GetString(string[] cs)
{
if (cs.Length == 2) {
return new string[] { cs[0] + cs[1], cs[1] + cs[0] };
}
List<string> list = new List<string>();
//上面这里多了-1,我多手
for (int i = 0; i < cs.Length; i++)
{
List<string> tmp=new List<string>(cs);
tmp.RemoveAt(i);
string[] sub=GetString(tmp.ToArray());
List<string> tmp2 = new List<string>();
foreach (string s in sub)
{
tmp2.Add(cs[i] + s);
}
list.AddRange(tmp2.ToArray());
}
return list.ToArray();
}
}
}
ABECDF
ABEFCD
AFEBCD
AFECDB
BAECDF
BAEFCD
BFEACD
BFECDA
CDEABF
CDEAFB
CDEBAF
CDEBFA
CDEFAB
CDEFBA
FAEBCD
FAECDB
FBEACD
FBECDA
ABEDCF
ABEFDC
AFEBDC
AFEDCB
BAEDCF
BAEFDC
BFEADC
BFEDCA
DCEABF
DCEAFB
DCEBAF
DCEBFA
DCEFAB
DCEFBA
FAEBDC
FAEDCB
FBEADC
FBEDCA
尤其 list.AddRange(GetString(new string[] {"A","B","CD","F" }));这行嘿嘿还是老烦您尽量在每行加个注释~谢谢~
也就是说当传进字符串数是两个的时候~
比如:A,B =====就直接返回AB和BA ~~也就是递归的终点要做的事比如传进的是大于2的数量~~
比如:A,B,C
因为传B,C进去是返回BC和CB
那么就返回A+BC和A+CB,即ABC和ACB
很简单问题是只要熟悉递归就好了~~~