111,086
社区成员




static void Main(string[] args)
{
string chars = "ABC";
int step = 4;
Next(new List<string>(), chars.ToCharArray().ToList(), step - 1, String.Empty).ForEach(value => Console.WriteLine(value));
}
static List<string> Next(List<string> values, List<char> chars, int step, string next)
{
if (step == 0) chars.ForEach(value => values.Add(next + value));
else chars.ForEach(value => Next(values, chars, step - 1, next + value));
//chars.ForEach(step == 0 ? (Action<char>)(value => values.Add(next + value)) : (Action<char>)(value => Next(values, chars, step - 1, next + value)));
return values;
}
static void Main(string[] args)
{
string chars = "ABC";
int step = 4;
Next(new List<string>(), chars.ToCharArray().ToList(), step - 1, String.Empty).ForEach(value => Console.WriteLine(value));
}
static List<string> Next(List<string> values, List<char> chars, int step, string next)
{
if (step == 0) chars.ForEach(value => values.Add(next + value));
else chars.ForEach(value => Next(values, chars, step - 1, next + value));
//Element.ForEach(NeedLength == 0 ? (Action<char>)(value => values.Add(lastValue + value)) : (Action<char>)(value => Next(Element, NeedLength - 1, values, lastValue + value)));
return values;
}
using System;
using System.Text;
public class IntConvert
{
public static string Itos(string charset, int value, int n = 1)
{
StringBuilder ret = new StringBuilder(10);
while (value > 0)
{
ret.Append(charset[value % charset.Length]);
value /= charset.Length;
}
//补位
var c = n - ret.Length;
ret.Append(charset[0], c);
//如果是用作进制转换,则需要加上最后的反转
//int lastChIdx = ret.Length - 1;
////反转字符串
//for (var i = 0; i < ret.Length / 2; ++i)
//{
// var tmp = ret[i];
// ret[i] = ret[lastChIdx - i];
// ret[lastChIdx - i] = tmp;
//}
return ret.ToString();
}
static void Main()
{
string charset = "ABC";
int n = 4;
int max = (int)Math.Pow(charset.Length, n);
for (int i=0;i< max;++i)
Console.WriteLine(Itos(charset, i, 4));
}
}
稍微改了下,删掉了冗余的判断..using System;
using System.Text;
public class IntConvert
{
public static string Itos(string charset, int value, int n = 1)
{
StringBuilder ret = new StringBuilder(10);
if (value == 0)
{
ret.Append(charset[0], n);
return ret.ToString();
}
while (value > 0)
{
ret.Append(charset[value % charset.Length]);
value /= charset.Length;
}
//补位
var c = n - ret.Length;
ret.Append(charset[0], c);
//如果是用作进制转换,则需要加上最后的反转
//int lastChIdx = ret.Length - 1;
////反转字符串
//for (var i = 0; i < ret.Length / 2; ++i)
//{
// var tmp = ret[i];
// ret[i] = ret[lastChIdx - i];
// ret[lastChIdx - i] = tmp;
//}
return ret.ToString();
}
static void Main()
{
string charset = "ABC";
int n = 4;
int max = (int)Math.Pow(charset.Length, n);
for (int i=0;i< max;++i)
Console.WriteLine(Itos(charset, i, 4));
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string Element = "ABC";
int NeedLength = 4;
Arrange(Element, NeedLength).ToList().ForEach(x => Console.WriteLine(x));
}
static IEnumerable<string> Arrange(string Element, int NeedLength)
{
IEnumerable<string> e = Element.Select(x => new string(new char[] { x }));
var r = e;
for (int i = 0; i < NeedLength - 1; i++)
r = from x in r join y in e on 1 equals 1 select x + y;
return r;
}
}
}
public class Program
{
static List<string> GetCombination(int length, char[] chars, List<string> strList) {
if (strList.Count == 0) {
strList.Add("");
}
if (strList[0].Length >= length) {
return strList;
}
List<string> newList = new List<string>(chars.Length * strList.Count);
foreach (var str in strList) {
foreach (var c in chars) {
newList.Add(str+c);
}
}
return GetCombination(length, chars, newList);
}
static void Main(string[] args) {
List<string> result = GetCombination(4, "ABCD".ToCharArray(), new List<string>());
foreach (var str in result) {
Console.WriteLine(str);
}
}
}
AAAA
AAAB
AAAC
AAAD
AABA
AABB
AABC
AABD
AACA
AACB
AACC
AACD
AADA
AADB
AADC
AADD
ABAA
ABAB
ABAC
ABAD
ABBA
ABBB
ABBC
ABBD
ABCA
ABCB
ABCC
ABCD
ABDA
ABDB
ABDC
ABDD
ACAA
ACAB
ACAC
ACAD
ACBA
ACBB
ACBC
ACBD
ACCA
ACCB
ACCC
ACCD
ACDA
ACDB
ACDC
ACDD
ADAA
ADAB
ADAC
ADAD
ADBA
ADBB
ADBC
AD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