在C#或任何已知的库中,是否有任何方式可以打印尺寸大小的所有可能性,例如在数组声明中?打印尺寸的所有可能性
例如:
[2, 2]
给出:
[0,0] [0,1] [0,2] [1,0] [1,1] [1,2] [2,0] [2,1] [2,2]
我想它为所有的维(1D,2D,3D,4D ...)
在C#或任何已知的库中,是否有任何方式可以打印尺寸大小的所有可能性,例如在数组声明中?打印尺寸的所有可能性
例如:
[2, 2]
给出:
[0,0] [0,1] [0,2] [1,0] [1,1] [1,2] [2,0] [2,1] [2,2]
我想它为所有的维(1D,2D,3D,4D ...)
我建议产生所有项目可能
private static IEnumerable<String> Ranges(params int[] ranges) {
int[] current = new int[ranges.Length];
bool hasItems = true;
yield return String.Format("[{0}]", String.Join(",", current));
while (hasItems) {
hasItems = false;
for (int i = 0; i < current.Length; ++i) {
if (current[i] < ranges[i]) {
hasItems = true;
current[i] = current[i] + 1;
for (int j = i - 1; j >= 0; --j)
current[j] = 0;
yield return String.Format("[{0}]", String.Join(",", current));
break;
}
}
}
}
你的样品:
int[] source = new int[] { 2, 2 };
// [0,0] [1,0] [2,0] [0,1] [1,1] [2,1] [0,2] [1,2] [2,2]
Console.Write(String.Join(" ", Ranges(source)));
另一个测试(3名维)
int[] source = new int[] { 2, 1, 3};
// [0,0,0] [1,0,0] [2,0,0] [0,1,0] [1,1,0] [2,1,0] [0,0,1] [1,0,1] [2,0,1] [0,1,1]
// [1,1,1] [2,1,1] [0,0,2] [1,0,2] [2,0,2] [0,1,2] [1,1,2] [2,1,2] [0,0,3] [1,0,3]
// [2,0,3] [0,1,3] [1,1,3] [2,1,3]
Console.Write(String.Join(" ", Ranges(source)));
编辑:如果你要使用实际的多维数组,你可以使用Linq来获得尺寸列表:
// d is 3D array
// please, notice, that type of the array (string) doesn't play any role
string[,,] d = new string[2, 3, 1];
// array of dimensions: int[] {2, 3, 1}
int[] dims = Enumerable
.Range(0, d.Rank)
.Select(dim => d.GetLength(dim))
.ToArray();
Console.Write(String.Join(" ", Ranges(dims)));
你可以工作使它成为像这样的东西:
void IndexPermutations(int range1, int range2)
{
for(int i=0; i<range1; i++)
{
for(int j=0; j<range2; j++)
{
Console.WriteLine("[{0},{1}]",i,j);
}
}
}
这只是一个维度... –
因此,我们必须做他的整个作业吗? –
不,我们不需要 –
您可以使用递归解决方案。
public void IndexPermutations(int dimensionSize, int minValue, int maxValue, Action<List<int>> action)
{
IndexPermutationsInternal(dimensionSize, minValue, maxValue, action, new List<int>());
}
private void IndexPermutationsInternal(
int dimensionSize,
int minValue,
int maxValue,
Action<List<int>> action,
List<int> current)
{
if (dimensionSize == current.Count)
{
action(current);
}
else
{
for (int i = minValue; i <= maxValue; i++)
{
current.Add(i);
IndexPermutationsInternal(dimensionSize, minValue, maxValue, action, current);
current.RemoveAt(current.Count - 1);
}
}
}
有点IEnumerable的魔法!
public static IEnumerable<IEnumerable<int>> GetCombinations(IEnumerable<int> dimensions)
{
if (!dimensions.Any())
{
yield return Enumerable.Empty<int>();
yield break;
}
var first = dimensions.First();
foreach (var subSolution in GetCombinations(dimensions.Skip(1)))
{
for (var i = 0; i < first + 1; i++)
{
yield return new[] { i }.Concat(subSolution);
}
}
}
一个简单的解决方案,它从任何多维数组动态地生成的索引输出:
static void Main(string[] args)
{
int[,] arr = new int[2, 2];
// int[,,] arr = new int[3, 2, 3];
printArrayIndexes(arr);
}
private static void printArrayIndexes(object arr)
{
var dimensArr = arr as Array;
List<int> indexList = new List<int>();
for (int dimension = 0; dimension < dimensArr.Rank; dimension++)
{
indexList.Add(0);
}
bool hasItems = true;
while (hasItems)
{
hasItems = false;
Console.WriteLine(String.Format("[{0}]", String.Join(",", indexList)));
for (int i = 0; i < indexList.Count; i++)
{
if (indexList[i] < dimensArr.GetLength(i)) {
hasItems = true;
indexList[i]++;
break;
} else {
indexList[i] = 0;
}
}
}
}
使用您的样品int[,] arr = new int[2,2]
:
[0,0] [1,0] [2,0] [0,1] [1,1] [2,1] [0,2] [1,2] [2,2]
使用任何3D阵列int[,,] arr = new int[3,2,3]
:
[0,0,0] [1,0,0] [2,0,0] [3,0,0] [0,1,0] [1,1,0] [2,1,0] [3,1,0]
[0,2,0] [1,2,0] [2,2,0] [3,2,0] [0,0,1] [1,0,1] [2,0,1] [3,0,1]
[0,1,1] [1,1,1] [2,1,1] [3,1,1] [0,2,1] [1,2,1] [2,2,1] [3,2,1]
[0,0,2] [1,0,2] [2,0,2] [3,0,2] [0,1,2] [1,1,2] [2,1,2] [3,1,2]
[0,2,2] [1,2,2] [2,2,2] [3,2,2] [0,0,3] [1,0,3] [2,0,3] [3,0,3]
[0,1,3] [1,1,3] [2,1,3] [3,1,3] [0,2,3] [1,2,3] [2,2,3] [3,2,3]
所以你可以使用一维,二维,三维,四维阵列...
只要知道107MP,如果你使用这个,你的讲师就会知道*你肯定没有自己写。 –
谢谢,我没有要求解决方案。但我虽然这会很难做到。我感谢你的努力。 – 107MP
@BradleyUffner这不是一项功课;) – 107MP