数组全排列

using System.Collections.Generic;

class FullArray
{
	private int Index = 0;
	private Dictionary<int ,string> dic;

	public FullArray()
	{
		dic = new Dictionary<int, string> ();
	}
	/// <summary>
	/// 数组全排列
	/// </summary>
	/// <param name="listdata">Listdata.</param>
	/// <param name="s">起始索引.</param>
	/// <param name="e">结束索引.</param>
	public Dictionary<int ,string> permlist(int[] listdata, int s, int e)
	{
		if (s == e) {
			string str = "";
			for (int i = 0; i <= e; i++)
				str += listdata [i].ToString () + ",";

			//System.Console.WriteLine (Index + " | " + str.TrimEnd (',')); //输出测试
			dic.Add (Index, str.TrimEnd (',')); //存入字典
			Index += 1; //索引递增
		} else {
			for (int i = s; i <= e; i++) {
				Swap (ref listdata [s], ref listdata [i]);
				permlist (listdata, s + 1, e);
				Swap (ref listdata [s], ref listdata [i]);
			}
		}

		return dic;
	}
	public  void Swap(ref int a,ref int b)
	{
		int temp = a;
		a = b;
		b = temp;
	}		
}


using System.Linq;

int n = 3;
				int[] arr = Enumerable.Range (0, n + 1).ToArray ();
				IEnumerable<IEnumerable<int>> query = arr.Select (x => new int[] { x });
				while (query.First().Count() != arr.GetLength(0))
					query = query.SelectMany (x => arr.Where (y => !x.Contains (y)).Select (y => x.Concat (new int[] { y })));
				foreach (var item in query)
					Console.WriteLine (string.Join (",", item.Select (x => x.ToString ()).ToArray ())); 
/// <summary>
		/// 获取全排列数组
		/// </summary>
		public List<int[]> GetFullarray(int N)
		{
			int[] arr = GetIntArr (N);

			IEnumerable<IEnumerable<int>> query = arr.Select (x => new int[] { x });
			while (query.First ().Count () != arr.GetLength (0))
				query = query.SelectMany (x => arr.Where (y => !x.Contains (y)).Select (y => x.Concat (new int[] { y })));

			List<int[]> Reg = new List<int[]> ();
			foreach (var item in query)
				Reg.Add (item.Select (x => x).ToArray ());

			return Reg;
		}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值