在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 ......)
答案 0 :(得分:4)
我建议生成所有可能的项目
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)));
答案 1 :(得分:2)
一点点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);
}
}
}
答案 2 :(得分:2)
一个简单的解决方案,可以从任何多维数组动态生成索引输出:
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]
所以你可以使用1D,2D,3D,4D阵列......
答案 3 :(得分:1)
你可以自己做这样的事情:
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);
}
}
}
答案 4 :(得分:1)
您可以使用递归解决方案。
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);
}
}
}