【问题标题】:How to carry out operations on random amount of lists which contain random amount of elements如何对包含随机数量元素的随机数量列表执行操作
【发布时间】:2012-03-18 17:39:06
【问题描述】:

我有许多链接列表,其中链接列表的数量在理论上是随机的,因为我可以有一个未指定的数量。每个列表中的元素数量也可以是任意数量。基本上,我可以拥有随机数量的列表,其中包含随机数量的元素,如下所示:

列表 1

  • 1
  • 34
  • 91

清单 2

  • 6
  • 5
  • 94
  • 43
  • 245
  • 467

清单 3

  • 98
  • 39

清单 4

  • 11

等等……

我想做的是检查每个添加在一起的列表中的任何元素组合是否等于某个值。

例如,List 1 中的元素 1 + List 2 中的元素 5 + List 3 中的元素 2 + List 4 中的元素 1 = 某个值。我想要的是前一个列表之后的所有列表组合,即它必须是列表 1 + 列表 2 + .... 列表 n,按顺序。

有人能建议如何实现吗?

【问题讨论】:

  • 如果是家庭作业,我会适当地标记它。仅仅因为我分解了一个问题并试图简化它以获得更好的帮助机会,它并不能成为家庭作业。
  • 看起来类似于这个问题:stackoverflow.com/questions/9754302/…。它不是特定于 C# 的,但从概念上讲,问题看起来是一样的。

标签: c# .net list data-structures linked-list


【解决方案1】:

如果你说的是:从每个列表中取一个 int,看看它们的总和是否等于某个值,那么

List<List<int>> lists = new List<List<int>>();
lists.Add(new List<int>() { 1, 34, 91});
lists.Add(new List<int>() { 6, 5, 94, 43, 245, 467 });
lists.Add(new List<int>() { 98, 39 });
lists.Add(new List<int>() { 11 });

var cp = CartesianProduct(lists);
int searchFor = 115;
foreach (var list in cp)
{
    if (list.Sum() == searchFor)
    {
        foreach (var i in list) Console.Write(i + " ");
    }
}

public static IEnumerable<IEnumerable<T>> CartesianProduct<T>(IEnumerable<IEnumerable<T>> sequences)
{
    IEnumerable<IEnumerable<T>> emptyProduct = new IEnumerable<T>[] { Enumerable.Empty<T>() };
    return sequences.Aggregate(
        emptyProduct,
        (accumulator, sequence) =>
        {
            return accumulator.SelectMany(
                (accseq => sequence),
                (accseq, item) => accseq.Concat(new T[] { item })
            );
        }
    );
}

【讨论】:

    【解决方案2】:

    试试这个:

    const int SearchedValue = 296;
    static List<LinkedList<int>> lists;
    static Stack<int> indexes = new Stack<int>();
    
    private static void Check(int sum, int listIndex)
    {
        if (listIndex == lists.Count)
        {
            if (sum == SearchedValue)
            {
                Console.WriteLine("Found: " + String.Join(", ",
                    indexes.Reverse().Select(i => i + 1).ToArray()));
            }
        }
        else
        {
            int i = 0;
            foreach (var value in lists[listIndex])
            {
                indexes.Push(i++);
                Check(sum + value, listIndex + 1);
                indexes.Pop();
            }
        }
    }
    
    public static void Main()
    {
        var list1 = new LinkedList<int>();
        list1.AddLast(1);
        list1.AddLast(34);
        list1.AddLast(91);
    
        var list2 = new LinkedList<int>();
        list2.AddLast(6);
        list2.AddLast(5);
        list2.AddLast(94);
        list2.AddLast(43);
        list2.AddLast(245);
        list2.AddLast(467);
    
        var list3 = new LinkedList<int>();
        list3.AddLast(98);
        list3.AddLast(39);
    
        var list4 = new LinkedList<int>();
        list4.AddLast(11);
    
        lists = new List<LinkedList<int>>() { list1, list2, list3, list4 };
    
        Check(0, 0);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 2015-08-20
      • 1970-01-01
      • 1970-01-01
      • 2022-12-31
      相关资源
      最近更新 更多