动态嵌套循环生成数组中的元素在C#中所有可能的组合 [英] Dynamic nested loops to generate all possible combinations of array elements in c#

查看:330
本文介绍了动态嵌套循环生成数组中的元素在C#中所有可能的组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索计算器和网络的类似的问题,但我无法找到我需要的解决方案。

I've searched stackoverflow and the web for similar questions, but i couldn't found the solution that i need.

我在编程code-列表生成器。

I'm programming a code-list-generator.

因此​​,例如我有字符的目录如名单,其中,焦炭> {A,B,C};

so for example i have a List of chars like List<char> { 'a', 'b', 'c' };.

和我有一对夫妇的设置,如(INT)的minLength 2 和一个(INT)最大长度 3 。

and i have a couple of settings like (int)minLength of 2 and a (int)maxLength of 3.

和我想要的输出:

aa
ab
ac
ba
bb
bc
ca
cb
cc

aaa
aab
aac
aba
abb
abc
aca
acb
acc

baa
bab
bac
bba
bbb
bbc
bca
bcb
bcc

caa
cab
cac
cba
cbb
cbc
cca
ccb
ccc

在gereral我只想kreate多维循环,但我必须做的diferent的minLength,最大长度和放大器的动态,因为这一点; charlist中的值。

in gereral i would just kreate multidimensional loops, but i have to do this dynamically because of the diferent minLength, maxLength & charList values.

所以我会用一个自我调用函数像这样的例子:

so i was going with a "self-calling-function" like this example:

    private void loop() {
        for( int i = 0; i < num; i++ ) {
            // stuff
            loop();
        }
    }

到目前为止,我下面的一堆code制成,但在这个阶段,我被卡住...:

so far i've made following bunch of code, but in this stage i get stuck... :

    Thread mainThread;

    List<char> azlower;
    List<char> azupper;
    List<char> nullnine;

    List<char> totalChars;

    int totalNum;

    int levelCounter;

    bool running;

    public Form1() {
        InitializeComponent();
    }

    private void init() {
        azlower = new List<char> { 'a', 'b', 'c' , 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
        azupper = new List<char> { 'A', 'B', 'C' , 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
        nullnine = new List<char> { '0', '1', '2' /* , '3', '4', '5', '6', '7', '8', '9' */ };

        totalChars = new List<char> ();

        running = false;
    }

    private void button1_Click( object sender, EventArgs e ) {
        if( !running ) {
            init();

            // Start
            if( checkBoxAZ1.Checked ) {
                foreach( char character in azlower ) {
                    totalChars.Add( character );
                }
            }
            if( checkBoxAZ2.Checked ) {
                foreach( char character in azupper ) {
                    totalChars.Add( character );
                }
            }
            if( checkBox09.Checked ) {
                foreach( char character in nullnine ) {
                    totalChars.Add( character );
                }
            }
            if( checkBoxS.Checked && textBoxSpec.Text != "" ) {
                char[] specArray = textBoxSpec.Text.ToCharArray();
                foreach( char character in specArray ) {
                    totalChars.Add( character );
                }
            }
            totalNum = totalChars.Count;
            levelCounter = Int32.Parse( textBoxMinLength.Text );
            mainThread = new Thread( new ThreadStart( run ) );
            button1.Text = "Stop";
            running = true;
            mainThread.Start();
        } else {
            mainThread.Abort();
            button1.Text = "Start";
            running = false;
        }
    }

    private void run() {
        for( int i = 0; i < totalNum; i++ ) {
            Invoke( ( MethodInvoker ) delegate {
                write( totalChars[ i ].ToString() );
                if( i == totalNum - 1 && levelCounter == Int32.Parse( textBoxMaxLength.Text ) ) {
                    write( "\n" );
                }
            } );
            if( levelCounter < Int32.Parse( textBoxMaxLength.Text ) ) {
                levelCounter++;
                run();
            }
        }
        return;
    }

    private void write( string line ) {
        richTextBox1.Text += line;
    }

但与上面的设置,我的code,输出看起来是这样的:

but with the setup above, and my code, the output looks like this:

aabc
bc

我想我只是做一个有思想的错误,不是吗?

i think i've just made a thinking mistake, didn't i ?

这样的家伙,你有ANNY建议我?

so guys, do you have anny suggestions for me ?

我也看了看笛卡尔乘积,但我认为它不会只用一个阵列工作...

i've also looked at the Cartesian Product, but i thought that it would not work with only one array...

THX的任何帮助。

推荐答案

下面是将使用递归生成它的一个例子。

Here is an example that would generate it using recursion.

IEnumerable<string> GenerateCode(int length, int min, IEnumerable<char> chars)
{
    if (length == 0)
    {
        yield return string.Empty;
        yield break;
    }

    foreach (var mid in GenerateCode(length - 1, min, chars))
    {
        foreach (var c in chars)
        {
            var t = mid + c;
            if (length >= min)
                Console.WriteLine(t); // replace with where you want to put the results

            yield return t;
        }
    }
}

// now call this method:
GenerateCode(3 /*max length*/, 2 /*min length*/, new [] { 'a', 'b', 'c'  });

但是,除非这是一个锻炼,你为什么会生成所有可能的变化?有可能是一个更好的解决办法,如果你提供的实际需要。

But unless this is an exercise, why would you generate all possible variations? There might be a much better solution if you provide the actual requirement.

这篇关于动态嵌套循环生成数组中的元素在C#中所有可能的组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆