Bootstrap

c# 递归算法获取数组里边所有组合

  private int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 };
    private int num = 11;
    private int[] result = null;
    private int index = 0;
    // Start is called before the first frame update
    void Start () {

        result = new int[num];
        GetCombiNationMod (arr, result, 0, num);
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="_arr">原始数组</param>
    /// <param name="_result">存储结果的数组</param>
    /// <param name="_start">开始遍历的索引</param>
    /// <param name="_count">提取几个元素</param>
    private void GetCombiNationMod (int[] _arr, int[] _result, int _start, int _count) {

        for (int i = _start; i < _arr.Length; i++) {
            _result[_count - 1] = i;

            if ((_count - 1) == 0) {
                var v = "";
                for (int j = _result.Length - 1; j >= 0; j--) {
                    v += _arr[_result[j]].ToString ();
                }
                index++;
                print (index + " " + v);
            } else {
                GetCombiNationMod (_arr, _result, i + 1, _count - 1);
            }
        }
    }

参考资料:https://blog.csdn.net/qq_33913037/article/details/72867772?utm_medium=distribute.pc_relevant_bbs_down.none-task-blog-baidujs-1.nonecase&depth_1-utm_source=distribute.pc_relevant_bbs_down.none-task-blog-baidujs-1.nonecase

;