zoukankan      html  css  js  c++  java
  • 用C#写一个函数,在一个数组中找出随意几个值相加等于一个值 与迭代器对比

    算法!用C#写一个函数,在一个数组中找出随意几个值相加等于一个值
    比如,数组{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}  
    要找出那些数相加等于100

     解决方案一:
          #region  解决方案一
                int[] myarray = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
                List<List<int>> mylist = new List<List<int>>();
                int length = myarray.Length;
                for (int i = 0; i < Math.Pow(2, length); i++)
                {
                    List<int> myint = new List<int>();
                    for (int j = 0; j < length; j++)
                    {
                        if (Convert.ToBoolean(i & (1 << j)))
                            myint.Add(myarray[j]);
                    }
                    mylist.Add(myint);
                }
                foreach (var a in mylist)
                {
                    if (a.Sum() == 100)
                    {
                        foreach (var b in a)
                        {
                            Console.Write(b); Console.Write(",");
                        }
                        Console.WriteLine();
                    }
                }
                Console.ReadKey();
                Console.WriteLine("--------------");
               #endregion
     解决方案二:使用迭代器
    迭代器(iterator)是一种对象,它能够用来遍历标准模板库容器中的部分或全部元素,每个迭代器对象代表容器中的确定的地址。
    迭代器修改了常规指针的接口,所谓迭代器是一种概念上的抽象:那些行为上像迭代器的东西都可以叫做迭代器。然而迭代器有很多不同的能力,
    它可以把抽象容器和通用算法有机的统一起来。

     static void Main(string[] args)
            {
    double[] myarray1 = { 1.1, 13.6, 14.8, 15.9, 16.4, 17.7, 2.5, 3.3, 4.9, 5.8, 6.8, 7.5, 8.7, 9.4, 10.7, 11.2, 12.9, 13.6, 14.8, 15.9, 16.4, 17.7, 18.4, 19.2 };
                int cnt = 0;
                foreach (var result in SelectCompute(myarray1, 100.0, myarray1.Length - 1))
                {
                    Console.Write("结果{0}==> ", ++cnt);
                    foreach (var n in result)
                        Console.Write("{0} ", n);
                    Console.WriteLine();
                }
                Console.WriteLine("--------------End");
                Console.ReadKey();
                #endregion
            }
    
    
            /// <summary>
            /// 寻找组合   迭代器算法
            /// </summary>
            /// <param name="array">数组</param>
            /// <param name="sum">总数</param>
            /// <param name="index"></param>
            /// <returns></returns>
            static IEnumerable<IEnumerable<double>> SelectCompute(double[] array, double sum, int index)
            {
                if (index >= 0)
                {
                    foreach (var sub in SelectCompute(array, sum, index - 1))
                        yield return sub;
    
                    foreach (var sub in SelectCompute(array, sum - array[index], index - 1))
                        yield return sub.Concat(new double[] { array[index] });
    
                    if (Math.Abs(array[index] - sum) <= double.Epsilon)
                        yield return new double[] { array[index] };
                }
            }
  • 相关阅读:
    URAL——DFS找规律——Nudnik Photographer
    URAL1353——DP——Milliard Vasya's Function
    URAL1203——DPor贪心——Scientific Conference
    递推DP HDOJ 5389 Zero Escape
    区间DP UVA 1351 String Compression
    树形DP UVA 1292 Strategic game
    Manacher HDOJ 5371 Hotaru's problem
    同余模定理 HDOJ 5373 The shortest problem
    递推DP HDOJ 5375 Gray code
    最大子序列和 HDOJ 1003 Max Sum
  • 原文地址:https://www.cnblogs.com/muyeh/p/8808059.html
Copyright © 2011-2022 走看看