# 求个算法，M个数，取N个排列，N>M

marmothac 2021-04-30 10:37:21

1 1 1 1
1 1 1 2
1 1 1 3
1 1 2 2
1 1 2 3
1 1 3 3
1 2 2 2
1 2 2 3
1 2 3 3
1 3 3 3
2 2 2 2
2 2 2 3
2 2 3 3
2 3 3 3
3 3 3 3

...全文
2437 21 打赏 收藏 举报

21 条回复

marmothac 2021-05-05

• 打赏
• 举报

yyfhz 2021-05-05

groovy2007的解答或许不是最优，但确实是对的。

``````
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]'''
``````
• 打赏
• 举报

qishewl 2021-05-04

• 打赏
• 举报

Harrison_2009 2021-05-02

• 打赏
• 举报

https://blog.csdn.net/goldenhawking/article/details/80037669
• 打赏
• 举报

groovy2007 2021-05-01

``````
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]'''
``````
• 打赏
• 举报

marmothac 2021-04-30

• 打赏
• 举报

marmothac 2021-04-30

O(n^m) 递归,n=3,m=4，得到81个数。 不会递归直接写4层循环。

• 打赏
• 举报

wanghui0380 2021-04-30

• 打赏
• 举报

wanghui0380 2021-04-30

• 打赏
• 举报

guanyinsishengzi 2021-04-30
O(n^m) 递归,n=3,m=4，得到81个数。 不会递归直接写4层循环。
• 打赏
• 举报

• 打赏
• 举报

wanghui0380 2021-04-30

• 打赏
• 举报

marmothac 2021-04-30

• 打赏
• 举报

xuzuning 2021-04-30
P(M,N) 是 从 M 个 元素中取 N个做排列 那么 你能从 4 个数中取出 5 个数吗
• 打赏
• 举报

morliz子轩 2021-04-30
demo:
``````
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);
}

``````
• 打赏
• 举报

wanghui0380 2021-04-30

• 打赏
• 举报

marmothac 2021-04-30

• 打赏
• 举报

wanghui0380 2021-04-30

[quote=引用 6 楼 wanghui0380 的回复:]哦，的确是个障眼法，我也迷了，不过结论正确，压根不值得考虑 上面被迷，修复一下，还是M进制 谁说10进制数不能表达11位数据了，所以压根不要去考虑撒N》M，这条件就是用来迷惑人的
M>N,还是M<N，是有点说法的，实现起来还真不是一样的[/quote] 没你想的那么复杂，你弄明白了，他就是进位随便写，反正要放假了，无心写代码，俺就随手写一个，也不讲究啥效率，性能，最优。你看看，到底所谓的N，M大小对你实现有没有啥区别，反正对于我下面的代码来说，没有任何区别
`````` 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 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
{

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;
}
}

}
``````

• 打赏
• 举报

xuzuning 2021-04-30

• 打赏
• 举报

10.8w+

.NET技术 C#

2021-04-30 10:37