数组项的排列

发布于 2024-10-11 00:56:45 字数 287 浏览 4 评论 0原文

如何获得计数为 2 的字符串数组的组合? IE。

List<string> myString = {"a", "b", "c", "d", "f"};

排列如下所示:

ab 交流电 广告 AF 巴 公元前 BD 男朋友 加州 CB 光盘 比照 等等...

我不知道如何开始这个算法。如果有帮助,我宁愿做一个循环而不是递归,因为在我的实际实现中,我必须为排列的项目分配一个值,并将每个项目与另一个项目进行比较并选择最高的。

How do I go about getting a combination of an array of strings of count 2? Ie.

List<string> myString = {"a", "b", "c", "d", "f"};

A permutation would look like this:

ab
ac
ad
af
ba
bc
bd
bf
ca
cb
cd
cf

etc...

I have no idea how to begin this algorithm. If it helps, I'd rather do a loop than a recursion because in my actual implementation, I have to assign a value to the permuted items and compare each one to another and select the highest.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

同尘 2024-10-18 00:56:45

使用 Linq:

var result = 
    from a in myString
    from b in myString
    where a != b
    select a + b;

Using Linq:

var result = 
    from a in myString
    from b in myString
    where a != b
    select a + b;
两相知 2024-10-18 00:56:45

不使用 LINQ

List<string> myString = {"a", "b", "c", "d", "f"};
List<String> result = new List<String> ();
for (int i = 0; i < myString.Count; i++)
{
    for (int j = 0; j < myString.Count; j++)
    {
        if (i == j)
            continue;
        result.Add(myString[i] + myString[j]);
    }
}

Not using LINQ

List<string> myString = {"a", "b", "c", "d", "f"};
List<String> result = new List<String> ();
for (int i = 0; i < myString.Count; i++)
{
    for (int j = 0; j < myString.Count; j++)
    {
        if (i == j)
            continue;
        result.Add(myString[i] + myString[j]);
    }
}
风流物 2024-10-18 00:56:45

如果没有 LINQ,您可以使用嵌套循环:

var permutations = new List<string>();
for(int i = 0; i < myString.Count; i++) 
{
    for(int j = 0; j < myString.Count; j++)
    {
        if(i == j)
            continue;

        var permutation = string.Format("{0}{1}", myString[i], myString[j]);
        permutations.Add(permutation);
    }
}

Without LINQ, you can use a nested loop:

var permutations = new List<string>();
for(int i = 0; i < myString.Count; i++) 
{
    for(int j = 0; j < myString.Count; j++)
    {
        if(i == j)
            continue;

        var permutation = string.Format("{0}{1}", myString[i], myString[j]);
        permutations.Add(permutation);
    }
}
旧情勿念 2024-10-18 00:56:45

补充一下之前的答案:

        string[] arreglo = new string[6];
        arreglo[0] = "a";
        arreglo[1] = "b";
        arreglo[2] = "c";
        arreglo[3] = "d";
        arreglo[4] = "e";
        arreglo[5] = "f";

        var permutations = new List<string>();
        for (int i = 0; i < arreglo.Length; i++)
        {
            for (int j = 0; j < arreglo.Length; j++)
            {
                for (int k = 0; k < arreglo.Length; k++)
                {
                    for (int l = 0; l < arreglo.Length; l++)
                    {
                        for (int m = 0; m < arreglo.Length; m++)
                        {
                            for (int n = 0; n < arreglo.Length; n++)
                            {
                                if (i ==j ||j == k||i == k||k == l||i == l||j == l||i == m||j == m||k == m||l == m||i == n||j == n||k == n||l == n||m == n)
                                    continue;

                                var permutation = string.Format("{0}{1}{2}{3}{4}{5}", arreglo[i], arreglo[j], arreglo[k], arreglo[l], arreglo[m],arreglo[n]);
                                permutations.Add(permutation);
                            }
                        }
                    }
                }
            }
        }

        foreach(var element in permutations)
        {
            Console.WriteLine(element);
        }
        Console.ReadLine();

Complementing the previous answers:

        string[] arreglo = new string[6];
        arreglo[0] = "a";
        arreglo[1] = "b";
        arreglo[2] = "c";
        arreglo[3] = "d";
        arreglo[4] = "e";
        arreglo[5] = "f";

        var permutations = new List<string>();
        for (int i = 0; i < arreglo.Length; i++)
        {
            for (int j = 0; j < arreglo.Length; j++)
            {
                for (int k = 0; k < arreglo.Length; k++)
                {
                    for (int l = 0; l < arreglo.Length; l++)
                    {
                        for (int m = 0; m < arreglo.Length; m++)
                        {
                            for (int n = 0; n < arreglo.Length; n++)
                            {
                                if (i ==j ||j == k||i == k||k == l||i == l||j == l||i == m||j == m||k == m||l == m||i == n||j == n||k == n||l == n||m == n)
                                    continue;

                                var permutation = string.Format("{0}{1}{2}{3}{4}{5}", arreglo[i], arreglo[j], arreglo[k], arreglo[l], arreglo[m],arreglo[n]);
                                permutations.Add(permutation);
                            }
                        }
                    }
                }
            }
        }

        foreach(var element in permutations)
        {
            Console.WriteLine(element);
        }
        Console.ReadLine();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文