算法:求序列中和为特定值的集合

作者: 没了帽子的Link | 来源:发表于2017-12-26 16:41 被阅读13次

想法是利用递归的方式来解决。每次都从序列中循环出一个数与特定值做比较,三种情况:

        ①    等于    ---    存进结果数组

        ②    小于    ---    扔进递归,特定值为父递归的特定值减去此次循环出的值

        ③    大于    ---    抛弃

在循环完成之后返回值之前给结果集去重。

之后判断剩下的结果集的自身的数相加是否都等于特定值,抛弃不等于者。

返回。

用C#实现的效果:


image.png

接下来是用C#实现的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Threading;

namespace test
{
    class Program
    {
        const int COUNT = 10;

        const int MIDDLE = -1;

        static int[] iArray;
        static void Main(string[] args)
        {
            //  随机生成序列
            Random random = new Random(DateTime.Now.Millisecond);
            iArray = new int[COUNT];
            for (int i = 0; i < COUNT; i++)
            {
                iArray[i] = random.Next(1, 10);
            }
            foreach (var i in iArray)
            {
                Console.Write(i + " ");
            }
            Console.WriteLine();

            //  指定一个特定数,并且找出序列中和为特定数的组合
            int s = 12;
            Console.WriteLine("特定值为:" + s);
            var r = Poling(s);
            foreach (var i in r)
            {
                if(i!=MIDDLE)
                Console.Write(" 第"+i+"号:"+iArray[i]);
                else
                    Console.WriteLine();
            }

            Console.ReadLine();
        }

        /// <summary>
        /// 递归-求出指定数在序列中和的组合
        /// </summary>
        /// <param name="x">指定数</param>
        /// <returns>组合集</returns>
        static List<int> Poling(int x)
        {
            //  轮查可以形成或等于指定数x的数集
            var _array = new List<int>();
            var result = new List<int>();
            for (int i = 0; i < iArray.Length; i++)
            {
                if (iArray[i] == x)     //可以形成
                {
                    result.Add(i);
                    result.Add(MIDDLE);
                }
                else if (iArray[i] < x)     //小于指定数,还需要继续寻找能够组成差值的数集
                {
                    var temp = Poling(x - iArray[i]);   //递归调用此方法
                    temp = Regroup(i, temp);    
                    result.AddRange(temp);
                }
            }

            //  去重
            result = RemoveRepeat(result);  

            //  把和不等于指定数x的数集删除
            int add = 0;
            List<int> _result = new List<int>();
            List<int> t = new List<int>();
            for (int i = 0; i < result.Count; i++)
            {
                if (result[i] == MIDDLE)
                {
                    if (add == x)
                    {
                        _result.AddRange(t);
                        _result.Add(MIDDLE);
                        t = new List<int>();
                        add = 0;
                    }
                }
                else
                {
                    t.Add(result[i]);
                    add += iArray[result[i]];
                }
            }

            //  返回值-返回值为iArray的序号(不为值)
            //  如 0,3,6,9,-1
            //  其中-1 为MIDDLE常量
            return _result;
        }


        /// <summary>
        /// 去重方法
        /// </summary>
        /// <param name="list">需要去重的数据</param>
        /// <returns></returns>
        static List<int> RemoveRepeat(List<int> list)
        {
            List<List<int>> cut = new List<List<int>>();
            List<int> temp = new List<int>();
            foreach (var i in list)
            {
                if (i == MIDDLE)
                {
                    cut.Add(temp);
                    temp = new List<int>();
                }
                else
                {
                    temp.Add(i);
                }
            }


            //  单列去重
            for (int i = 0; i < cut.Count; i++)
            {
                cut[i] = RemoveSignRepeat(cut[i]);
            }

            //  多列去重
            int count = cut.Count;
            for (int i = 0; i < count - 1; i++)
            {
                for (int j = i + 1; j < count; j++)
                {
                    if (IsRepeat(cut[i], cut[j]))
                    {
                        cut.Remove(cut[j]);
                        count--;
                    }
                }
            }

            var result = new List<int>();
            foreach (var i in cut)
            {
                foreach (var j in i)
                {
                    result.Add(j);
                }
                result.Add(MIDDLE);
            }
            return result;
        }

        /// <summary>
        /// 重组
        /// </summary>
        /// <param name="y"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        static List<int> Regroup(int y ,List<int> list)
        {
            var _list = new List<int>();
            for (int i = 0;i<list.Count;i++)
            {
                if (i == 0 || list[i] == MIDDLE)
                {
                    _list.Add(y);
                }
                _list.Add(list[i]);
            }
            return _list;
        }


        /// <summary>
        /// 判断两个序列是否相同
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        static bool IsRepeat(List<int> a ,List<int> b){
            int count =a.Count;
            int c=0;
            if(count ==b.Count){
                foreach (var i in a)
                {
                    foreach (var j in b)
                    {
                        if (i == j)
                            c++;
                    }
                }
            }
            if (c > count)
            {
                return true;
            }
            if (c == count)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 单列去重
        /// </summary>
        /// <param name="a"></param>
        /// <returns></returns>
        static List<int> RemoveSignRepeat(List<int> a)
        {
            List<int> result = new List<int>();
            for (int i = 0; i < a.Count; i++)
            {
                if (!result.Contains(a[i]))
                {
                    result.Add(a[i]);
                }
            }
            return result;
        }
    }
}

相关文章

  • 算法:求序列中和为特定值的集合

    想法是利用递归的方式来解决。每次都从序列中循环出一个数与特定值做比较,三种情况: 在循环完成之后返回值之前给结果集...

  • 美团面经合集

    面经1 给一个目标数 t,找出数组中和为t的组合(集合)有多少? 这个是很典型的贪心算法问题,代码如下 求集合的算...

  • 常用集合算法

    /* set_intersection算法 求两个set集合的交集 注意:两个集合必须是有序序列 @param b...

  • 最大子列和问题的4种复杂度算法

    定义:给定个整数的序列 ,求函数 的最大值(若最大子列和为负数,则返回0)。 算法1——int MaxSubSeq...

  • 360一面(已跪)

    1.先来了两个算法题 给一个无序的序列,序列中的数为整数(可正可负),求连续子序列的和的最大值。例:-8,1,3,...

  • 数据结构02 - 斐波那契(Fibonacci)数列问题分析

    问题优化分析 已知K阶斐波那契数列序列定义为 试编写求k求k阶斐波那契数列的第m项值的函数算法,k和m均以值调用的...

  • 第一章 基础知识

    什么是算法? 算法就是定义任何良性的计算过程,该过程区某个值或者值的集合作为输入并产生某个值或值集合作为输出。 为...

  • 最大子串问题

    给定N个整数的序列 {A1,A2, A3,...,AN}, 求函数 的最大值。 算法一 算法二 算法三分而治之...

  • java8-Stream-构建流

    stream 基础定义 元素序列——就像集合一样,流也提供了一个接口,可以访问特定元素类型的一组有序 值。因为集合...

  • NumPy学习

    矩阵操作 基本操作 向量 类型 取值 判断 矩阵 维度 求值 比较 特定赋值 类型转换 求最值 矩阵操作 生成序列...

网友评论

本文标题:算法:求序列中和为特定值的集合

本文链接:https://www.haomeiwen.com/subject/hhjjgxtx.html