【问题标题】:Combination of Elements with by Using Array使用数组组合元素
【发布时间】:2020-05-20 05:25:28
【问题描述】:

目标:
使用输入数据组合元素。目标是实现输出。

An example      

Current situation (input):      
jim west    aaa 12345
Le tomo     bbb 
Sara brown      

Requested Result(output):       
jim west    aaa 12345
Le tomo     aaa 12345
Sara brown  aaa 12345
jim west    bbb 
Le tomo     bbb 
Sara brown  bbb 

-------------------------------

Another example     

Current situation (input):      
jim west    aaa 12345
Le tomo     bbb 32154
Sara brown  ccc 78946

Requested Result(output):       
jim west    aaa 12345
Le tomo     aaa 12345
Sara brown  aaa 12345
jim west    bbb 32154
Le tomo     bbb 32154
Sara brown  bbb 32154
jim west    ccc 78946
Le tomo     ccc 78946
Sara brown  ccc 78946

-------------------------------

Another example

Current situation (input):              
jim west    aaa 12345
Le tomo     
Sara brown      

Requested Result(output):       
jim west    aaa 12345
Le tomo     aaa 12345
Sara brown  aaa 12345

问题:
*我不知道如何解决它。你对我应该使用什么路径有什么建议吗?

信息:
*我无法在这种情况下使用排列。

public class Program
{
    static void Main(string[] args)
    {

        string[] column1 = { "jim west", "Le tomo", "Sara brown" };
        string[] column2 = { "aaa", "bbb"};
        string[] column3 = { "12345" };



    }
}

【问题讨论】:

  • 请改进您的问题。提供示例数据而不是图像,提供预期的输出或类似的内容。
  • 我完全不知道你在问什么。你有 11k 的声望,我假设你现在知道如何提问。
  • 如果您需要更多解释,请告诉我。

标签: c# algorithm combinations combinatorics


【解决方案1】:

此代码将在示例中创建结果:

{
            var result = new List<string[]>();

            string[] column1 = { "jim west", "Le tomo", "Sara brown" };
            string[] column2 = { "aaa", "bbb" };
            string[] column3 = { "12345" };

            foreach(string c1 in column1)
            {
                for(int i = 0; i < column2.Length; i++)
                {
                    string[] toAdd = { c1, column2[i], column3.Length> i ? column3[i] : ""  };
                    result.Add(toAdd);
                }
            }

使用for而不是foreach的原因是colums2和colums3在例子中是连在一起的。如果它们不应该连接在一起; 3 嵌套的 foreach 会做这样的伎俩:

    var result = new List<string[]>();

    string[] column1 = { "jim west", "Le tomo", "Sara brown" };
    string[] column2 = { "aaa", "bbb" };
    string[] column3 = { "12345" };

    foreach(string c1 in column1)
    {

        foreach(string c2 in column2)
        {
            foreach(string c3 in column3)
            {
                string[] toAdd = { c1, c2, c3 };
                result.Add(toAdd);
            }
        }
    } 

【讨论】:

    猜你喜欢
    • 2013-05-15
    • 1970-01-01
    • 1970-01-01
    • 2021-09-10
    • 1970-01-01
    • 1970-01-01
    • 2017-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多