在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