c#语言.输出从数组a中取n个数的所有排列(可以重复)
答案:3 悬赏:40
解决时间 2021-02-11 16:29
- 提问者网友:践踏俘获
- 2021-02-11 02:44
c#语言.输出从数组a中取n个数的所有排列(可以重复)
最佳答案
- 二级知识专家网友:最后战士
- 2021-02-11 04:14
//题主没说明什么类型的数组作为输入,做了个泛型数组的例子
using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
class Program
{
private static void printArray(IEnumerable ou)
{
foreach (var o in ou)
{
foreach (var item in o)
{
Console.Out.Write(item);
Console.Out.Write(',');
}
Console.Out.WriteLine();
}
}
static void Main(string[] args)
{
//测试3个元素的int数组,取样2个的排列
//var testArray1 = ;
printArray(GetPermutationN(new int[] { 1, 2, 3 }, 2));
//测试4个元素的string数组,取样3个的排列
printArray(GetPermutationN(new string[] { "a", "b", "c", "d" }, 3));
}
public static IEnumerable GetPermutationN(T[] sourceArray, int permutationLength)
{
foreach (var index in getPermutationIndex(sourceArray.Length, permutationLength))
{
var rtnRow = new List();
foreach (var i in index)
rtnRow.Add(sourceArray[i]);
if (rtnRow.Count == permutationLength)
yield return rtnRow.ToArray();
}
}
///
/// 获取arrayLenght长度数组的permLength长度的排列
/// 所使用的乘方式递归函数
///
///
/// 样本元素数组的长度
///
///
/// 从样本数组中取样的个数
///
///
/// 返回样本元素数组下标矩阵
///
private static IEnumerable getPermutationIndex(int arrayLenght, int permLength)
{
for (int i = permLength; i > 0; i--)
{
if (i > 1)
{
var recursion = getPermutationIndex(arrayLenght, i - 1);
for (int j = 0; j < arrayLenght; j++)
foreach (var item in recursion)
{
var rtnRow = new List();
rtnRow.Add(j);
rtnRow.AddRange(item);
yield return rtnRow.ToArray();
}
}
else
for (int j = 0; j < arrayLenght; j++)
yield return new int[] { j };
}
}
}
}
using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
class Program
{
private static void printArray
{
foreach (var o in ou)
{
foreach (var item in o)
{
Console.Out.Write(item);
Console.Out.Write(',');
}
Console.Out.WriteLine();
}
}
static void Main(string[] args)
{
//测试3个元素的int数组,取样2个的排列
//var testArray1 = ;
printArray(GetPermutationN(new int[] { 1, 2, 3 }, 2));
//测试4个元素的string数组,取样3个的排列
printArray(GetPermutationN(new string[] { "a", "b", "c", "d" }, 3));
}
public static IEnumerable
{
foreach (var index in getPermutationIndex(sourceArray.Length, permutationLength))
{
var rtnRow = new List
foreach (var i in index)
rtnRow.Add(sourceArray[i]);
if (rtnRow.Count == permutationLength)
yield return rtnRow.ToArray();
}
}
///
/// 获取arrayLenght长度数组的permLength长度的排列
/// 所使用的乘方式递归函数
///
///
/// 样本元素数组的长度
///
///
/// 从样本数组中取样的个数
///
///
/// 返回样本元素数组下标矩阵
///
private static IEnumerable
{
for (int i = permLength; i > 0; i--)
{
if (i > 1)
{
var recursion = getPermutationIndex(arrayLenght, i - 1);
for (int j = 0; j < arrayLenght; j++)
foreach (var item in recursion)
{
var rtnRow = new List
rtnRow.Add(j);
rtnRow.AddRange(item);
yield return rtnRow.ToArray();
}
}
else
for (int j = 0; j < arrayLenght; j++)
yield return new int[] { j };
}
}
}
}
全部回答
- 1楼网友:许你一世温柔
- 2021-02-11 07:19
private void button1_Click(object sender, EventArgs e)
{
string[] a={"1","2","3"};
int n = 2;
object[] r = combine(a, n);
foreach (var item in r) Console.WriteLine(item.ToString());
}
private object [] combine( string[] sourceArray, int n)
{
if (n>1)
{
System.Collections .ArrayList r=new System.Collections.ArrayList ();
for (int i = 0; i < sourceArray.Length; i++)
{
object[] ar = combine(sourceArray, n-1);
for (int j = 0; j < ar.Length; j++)
{
ar[j] = sourceArray[i] + ar[j];
r.Add (ar[j]);
}
}
return r.ToArray ();
}
else
{
return sourceArray.ToArray ();
}
}
- 2楼网友:星星坠落
- 2021-02-11 05:39
因为可以重复,其实就是一个嵌套循环。
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯