English 中文(简体)
需要递归函数来生成唯一字符串组合。
原标题:
  • 时间:2009-02-07 10:48:35
  •  标签:

I have some(say 9, no not definite) unique strings from database(A,B,C,D,E,F,G,H) and I want to create unique combination of these fields to populate the listbox so that user can select single or different combination of these string fields like A,B,C,D,E,F,G,H, AB,AC,AD,AE,AF,AG,AH,AC,AD,AE,AF,AG,AG,AH,... ABC,ABD,ABE,ABF,ABG,ABH,ACD,ACE,ACF,ACG,ACH,.....

In C# (win application) Thanks in advance

问题回答

我首选的方法是“使用CheckedListBox,让用户自己选择”-这可以节省他们的理智...您是否愿意在长列表中寻找“ABCEFH”?

If you want the strings: How about just using binary arithmetic? i.e. use a number (bit-length as per the number of elements), and simply keep incrementing, including the set bits each time? So in C#, for example:

static void Main()
{
    foreach (string value in GetCombinations(
        "A", "B", "C", "D", "E", "F", "G", "H"))
    {
        Console.WriteLine(value);
    }
}
static IEnumerable<string> GetCombinations(params string[] tokens)
{
    ulong max = (ulong) 1 << tokens.Length;
    StringBuilder builder = new StringBuilder();
    // test all bitwise combinations
    for (ulong value = 0; value < max; value++)
    {
        builder.Length = 0;
        ulong tmp = value;
        // include the tokens for the set bits
        for (int i = 0; i < tokens.Length; i++)
        {
            if ((tmp & (ulong)1) == 1) builder.Append(tokens[i]);
            tmp >>= 1;
        }
        yield return builder.ToString();
    }
}

如果您希望按照您的示例顺序获取数据,则可使用 LINQ:

    foreach (string value in GetCombinations(
          "A", "B", "C", "D", "E", "F", "G", "H")
        .OrderBy(s=>s.Length)
        .ThenBy(s=>s))
    {
        Console.WriteLine(value);
    }

public class Combination { static int count = 0;

public static void main(String[] args)
{
    StringBuilder out = new StringBuilder("");
    StringBuilder str = new StringBuilder("aabcbd");

    combination(str, out, 0);
    System.out.println("The Count : " + count);
把这个翻译成中文:


// Recursive
static void combination(StringBuilder in, StringBuilder out, int start)
{
    int len = in.length();

    for (int i = start; i < len; i++)
    {
        if (isAppended(in, out, i))
        {
            continue;
        把这个翻译成中文:

        out.append(in.charAt(i));
        count++;
        System.out.println(out);
        combination(in, out, i + 1);
        out.deleteCharAt(out.length() - 1);
    把这个翻译成中文:
把这个翻译成中文:


static boolean isAppended(StringBuilder in, StringBuilder out, int index)
{
    int inCount = 0;
    int outCount = 0;

    int i = 0;
    int len = out.length();
    char ch = in.charAt(index);

    for (i = 0; i < index; i++)
    {
        if (in.charAt(i) == ch)
        {
            inCount++;
        把这个翻译成中文:
    把这个翻译成中文:

    for (i = 0; i < len; i++)
    {
        if (out.charAt(i) == ch)
        {
            outCount++;
        把这个翻译成中文:
    把这个翻译成中文:

    if (inCount != outCount)
    {
        return true;
    把这个翻译成中文:

    return false;
把这个翻译成中文:

把这个翻译成中文:

这就像在n进制下数整数一样……不应该难。





相关问题
热门标签