• 从数组中返回指定长度的子数组[转]


      1 using System;
      2 using System.Collections.Generic;
      3 using System.IO;
      4 using System.Runtime.Serialization;
      5 using System.Runtime.Serialization.Formatters.Binary;
      6 
      7 namespace ConsoleApplication3
      8 {
      9     class Program
     10     {
     11         static void Main(string[] args)
     12         {
     13             //要操作的源数组
     14             List<int> originalArray = new List<int>();
     15             for (int i = 1; i <= 10; i++)
     16             {
     17                 originalArray.Add(i);
     18             }
     19             Console.Write("源数组:");
     20             foreach (int item in originalArray)
     21             {
     22                 Console.Write(item + " ");
     23             }
     24             Console.WriteLine();
     25             //得到指定长度的子数组
     26             List<List<int>> resultArray = SubChildArray(originalArray, originalArray.Count - 1);
     27 
     28             for (int i = 0; i < resultArray.Count; i++)
     29             {
     30                 Console.Write("子数组" + (i + 1) + "");
     31                 for (int j = 0; j < resultArray[i].Count; j++)
     32                 {
     33                     Console.Write(resultArray[i][j] + " ");
     34                 }
     35                 Console.WriteLine();
     36             }
     37 
     38             Console.WriteLine("子数组的数量:" + resultArray.Count);
     39             Console.ReadKey();
     40         }
     41         /// <summary>
     42         /// 得到指定长度的子数组
     43         /// </summary>
     44         /// <param name="originalArray">源数组</param>
     45         /// <param name="arrayLength">要取出子数组的长度</param>
     46         /// <returns></returns>
     47         static List<List<int>> SubChildArray(List<int> originalArray, int arrayLength)
     48         {
     49             List<List<int>> result = new List<List<int>>();
     50             List<int> one = new List<int>();
     51 
     52             for (int i = 0; i < originalArray.Count; i++)
     53             {
     54                 one.Add(originalArray[i]);
     55                 Add(originalArray.GetRange(i + 1, originalArray.Count - 1 - i), arrayLength - 1, result, one);
     56                 if (one.Count != 0)
     57                     one.RemoveAt(one.Count - 1);
     58                 //这里很可以用one.Clear();
     59                 //循环完一次后,就可以清除one,然后再重新开始
     60             }
     61             return result;
     62         }
     63         /// <summary>
     64         /// 用于递归的函数
     65         /// </summary>
     66         /// <param name="array"></param>
     67         /// <param name="n"></param>
     68         /// <param name="result"></param>
     69         /// <param name="one"></param>
     70         static void Add(List<int> array, int n, List<List<int>> result, List<int> one)
     71         {
     72             //如果n=0,就表明one的Count属性已等于n
     73             if (n == 0)
     74             {
     75                 result.Add(Clone(one) as List<int>);
     76                 //进行下一次之前,移除最后一个元素
     77                 one.RemoveAt(one.Count - 1);
     78             }
     79             else
     80             {
     81                 for (int i = 0; i < array.Count; i++)
     82                 {
     83                     one.Add(array[i]);
     84                     //在这里进行递归,同时n-1
     85                     Add(array.GetRange(i + 1, array.Count - 1 - i), n - 1, result, one);
     86                 }
     87                 //一轮结束后,移除最后一个元素
     88                 one.RemoveAt(one.Count - 1);
     89             }
     90         }
     91         /// <summary>
     92         /// 用来复制对象的函数
     93         /// </summary>
     94         /// <param name="obj"></param>
     95         /// <returns></returns>
     96         public static object Clone(object obj)
     97         {
     98             using (MemoryStream ms = new MemoryStream())
     99             {
    100                 IFormatter formattor = new BinaryFormatter();
    101                 formattor.Serialize(ms, obj);
    102                 ms.Seek(0, SeekOrigin.Begin);
    103                 return formattor.Deserialize(ms);
    104             }
    105         }
    106     }
    107 }

     转自:http://www.cnblogs.com/xiangism/archive/2009/09/06/1561398.html

  • 相关阅读:
    内网穿透访问Vue项目的时候出现Invalid Host header解决办法
    Java中的super关键字
    java中的Arrays工具类及排序和查找
    Java中方法的覆盖/重写
    java中的数组
    java的内部类概述
    matlab主成分分析PCA
    matlab多元线性回归
    matlab神经网络工具箱
    神经网络(三):前馈神经网络(FNN)
  • 原文地址:https://www.cnblogs.com/qiyebao/p/4530413.html
Copyright © 2020-2023  润新知