111,119
社区成员
发帖
与我相关
我的任务
分享
def next(indexes, m, n):
for i in range(n):
index = indexes[i]
# find the first one that can be increased
if index < m-1:
# fill 0 .. i with the increased value
for j in range(i+1):
indexes[j] = index+1
return True
return False
def indexToNum(index, num):
return [num[i] for i in index]
nums = [1,2,3]
indexes = [0,0,0,0]
print(indexToNum(indexes, nums))
while next(indexes, 3, 4):
print(indexToNum(indexes, nums))
'''Output
[1, 1, 1, 1]
[2, 1, 1, 1]
[3, 1, 1, 1]
[2, 2, 1, 1]
[3, 2, 1, 1]
[3, 3, 1, 1]
[2, 2, 2, 1]
[3, 2, 2, 1]
[3, 3, 2, 1]
[3, 3, 3, 1]
[2, 2, 2, 2]
[3, 2, 2, 2]
[3, 3, 2, 2]
[3, 3, 3, 2]
[3, 3, 3, 3]'''
int[] array = {1, 2, 3, 4};
int a1, a2, a3, a4;
int iCount = 0;
for (int i1 = 0; i1 < array.Length; i1++)
{
for (int i2 = 0; i2 < array.Length; i2++)
{
if (i2 == i1)
{
continue;
}
for (int i3 = 0; i3 < array.Length; i3++)
{
if (i3 == i1 || i3 == i2)
{
continue;
}
for (int i4 = 0; i4 < array.Length; i4++)
{
if (i4 == i1 || i4 == i2 || i4 == i3)
{
continue;
}
a1 = array[i1];
a2 = array[i2];
a3 = array[i3];
a4 = array[i4];
iCount++;
Console.WriteLine("{0}-{1}-{2}-{3}", a1, a2, a3, a4);
}
}
}
}
Console.WriteLine("AllCount == {0}", iCount);
Console.ReadKey();
}
test test = new test(3, 4);
int[] dic = new int[] { 1, 2, 3 };
do
{
Console.WriteLine(string.Join(",", test.GetCcurentIndex().Reverse().Select(p => dic[p])));
} while (test.Increment());
public class test
{
private readonly int _m;
private readonly int _n;
private node nodel;
public test(int M, int N)
{
_m = M;
_n = N;
nodel = new node(M);
node ccurentnode = nodel;
for (int i = 0; i < _n - 1; i++)
{
node tempnode = new node(M);
ccurentnode.parent = tempnode;
ccurentnode = tempnode;
}
}
public bool Increment()
{
var lst = GetCcurentIndex();
if (!lst.All(p => p == _m - 1))
{
nodel.Increment();
return true;
}
else
{
return false;
}
}
public IEnumerable<int> GetCcurentIndex()
{
return Show(nodel);
}
private IEnumerable<int> Show(node node)
{
yield return node.Value;
if (node.parent != null)
{
var temp = Show(node.parent);
foreach (var item in temp)
{
yield return item;
}
}
else
{
yield break;
}
}
//位node
public class node
{
private readonly int _n;
public int Value { get; set; } = 0;
public node parent { get; set; }
public node(int N)
{
_n = N;
}
//模拟+1,判定是否要进位
public void Increment()
{
Value = Value + 1;
if (Value == _n)
{
parent?.Increment();
}
Value = Value % _n;
}
}
}
运行结果
1,1,1,1
1,1,1,2
1,1,1,3
1,1,2,1
1,1,2,2
1,1,2,3
1,1,3,1
1,1,3,2
1,1,3,3
1,2,1,1
1,2,1,2
1,2,1,3
1,2,2,1
1,2,2,2
1,2,2,3
1,2,3,1
1,2,3,2
1,2,3,3
1,3,1,1
1,3,1,2
1,3,1,3
1,3,2,1
1,3,2,2
1,3,2,3
1,3,3,1
1,3,3,2
1,3,3,3
2,1,1,1
2,1,1,2
2,1,1,3
2,1,2,1
2,1,2,2
2,1,2,3
2,1,3,1
2,1,3,2
2,1,3,3
2,2,1,1
2,2,1,2
2,2,1,3
2,2,2,1
2,2,2,2
2,2,2,3
2,2,3,1
2,2,3,2
2,2,3,3
2,3,1,1
2,3,1,2
2,3,1,3
2,3,2,1
2,3,2,2
2,3,2,3
2,3,3,1
2,3,3,2
2,3,3,3
3,1,1,1
3,1,1,2
3,1,1,3
3,1,2,1
3,1,2,2
3,1,2,3
3,1,3,1
3,1,3,2
3,1,3,3
3,2,1,1
3,2,1,2
3,2,1,3
3,2,2,1
3,2,2,2
3,2,2,3
3,2,3,1
3,2,3,2
3,2,3,3
3,3,1,1
3,3,1,2
3,3,1,3
3,3,2,1
3,3,2,2
3,3,2,3
3,3,3,1
3,3,3,2
3,3,3,3