求一排列算法!

alenx3 2004-10-21 05:42:42
给定两个字,M,N,在1到M里排出所有N位的排列

比如:给定10,5两个数
需要排列出
12345
12346
12347
.....
12349
21345
21346
...

数字不重复的
有点像福彩十选五的算法。
不,其实根本就是福彩十选五的算法。需要列出所有种组合

...全文
168 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
northwolves 2004-10-22
  • 打赏
  • 举报
回复
只要10 和5 ,可以用多重FOR 循环:

Private Sub Command1_Click()
Dim a As Integer, b As Integer, c As Integer, d As Integer, e As Integer
For a = 1 To 10
For b = 1 To 10
If b <> a Then
For c = 1 To 10
If c <> a And c <> b Then
For d = 1 To 10
If d <> a And d <> b And d <> c Then
For e = 1 To 10
If e <> a And e <> b And e <> c And e <> d Then
Debug.Print a; Space(1); b; Space(1); c; Space(1); d; Space(1); e
End If
Next
End If
Next
End If
Next
End If
Next
Next
End Sub
northwolves 2004-10-22
  • 打赏
  • 举报
回复
我找了个彩票软件看了看,双色球大赢家,33个球选6个,人家全部组合1107568注在我的破机子上才运行12秒不到,他是怎么实现的?用数据库吗?
-------------------
组合本来就比排列快得多

Private Sub Command1_Click()
pailie 15
End Sub
Sub pailie(ByVal m As Integer) 'select 5 elements from 1 to m
Dim result() As String, count As Long
Dim a As Integer, b As Integer, c As Integer, d As Integer, e As Integer
count = 0
For a = 1 To m
For b = 1 To m
If b <> a Then
For c = 1 To m
If c <> a And c <> b Then
For d = 1 To m
If d <> a And d <> b And d <> c Then
For e = 1 To m
If e <> a And e <> b And e <> c And e <> d Then
count = count + 1
ReDim Preserve result(1 To count)
result(count) = a & Space(1) & b & Space(1) & c & Space(1) & d & Space(1) & e
End If
Next
End If
Next
End If
Next
End If
Next
Next
MsgBox "ok"
Debug.Print Join(result, vbCrLf)
Debug.Print "count=" & count
End Sub
jam021 2004-10-22
  • 打赏
  • 举报
回复
关注,帮你顶!
alenx3 2004-10-22
  • 打赏
  • 举报
回复
我找了个彩票软件看了看,双色球大赢家,33个球选6个,人家全部组合1107568注在我的破机子上才运行12秒不到,他是怎么实现的?用数据库吗?
northwolves 2004-10-21
  • 打赏
  • 举报
回复
抱歉,答案有误。
northwolves 2004-10-21
  • 打赏
  • 举报
回复
10 6 4 8 1
10 6 4 9 1
10 6 5 1 2
10 6 5 2 1
10 6 5 3 1
10 6 5 4 1
10 6 5 7 1
10 6 5 8 1
10 6 5 9 1
10 6 7 1 2
10 6 7 2 1
10 6 7 3 1
10 6 7 4 1
10 6 7 5 1
10 6 7 8 1
10 6 7 9 1
10 6 8 1 2
10 6 8 2 1
10 6 8 3 1
10 6 8 4 1
10 6 8 5 1
10 6 8 7 1
10 6 8 9 1
10 6 9 1 2
10 6 9 2 1
10 6 9 3 1
10 6 9 4 1
10 6 9 5 1
10 6 9 7 1
10 6 9 8 1
10 7 1 2 3
10 7 1 3 2
10 7 1 4 2
10 7 1 5 2
10 7 1 6 2
10 7 1 8 2
10 7 1 9 2
10 7 2 1 3
10 7 2 3 1
10 7 2 4 1
10 7 2 5 1
10 7 2 6 1
10 7 2 8 1
10 7 2 9 1
10 7 3 1 2
10 7 3 2 1
10 7 3 4 1
10 7 3 5 1
10 7 3 6 1
10 7 3 8 1
10 7 3 9 1
10 7 4 1 2
10 7 4 2 1
10 7 4 3 1
10 7 4 5 1
10 7 4 6 1
10 7 4 8 1
10 7 4 9 1
10 7 5 1 2
10 7 5 2 1
10 7 5 3 1
10 7 5 4 1
10 7 5 6 1
10 7 5 8 1
10 7 5 9 1
10 7 6 1 2
10 7 6 2 1
10 7 6 3 1
10 7 6 4 1
10 7 6 5 1
10 7 6 8 1
10 7 6 9 1
10 7 8 1 2
10 7 8 2 1
10 7 8 3 1
10 7 8 4 1
10 7 8 5 1
10 7 8 6 1
10 7 8 9 1
10 7 9 1 2
10 7 9 2 1
10 7 9 3 1
10 7 9 4 1
10 7 9 5 1
10 7 9 6 1
10 7 9 8 1
10 8 1 2 3
10 8 1 3 2
10 8 1 4 2
10 8 1 5 2
10 8 1 6 2
10 8 1 7 2
10 8 1 9 2
10 8 2 1 3
10 8 2 3 1
10 8 2 4 1
10 8 2 5 1
10 8 2 6 1
10 8 2 7 1
10 8 2 9 1
10 8 3 1 2
10 8 3 2 1
10 8 3 4 1
10 8 3 5 1
10 8 3 6 1
10 8 3 7 1
10 8 3 9 1
10 8 4 1 2
10 8 4 2 1
10 8 4 3 1
10 8 4 5 1
10 8 4 6 1
10 8 4 7 1
10 8 4 9 1
10 8 5 1 2
10 8 5 2 1
10 8 5 3 1
10 8 5 4 1
10 8 5 6 1
10 8 5 7 1
10 8 5 9 1
10 8 6 1 2
10 8 6 2 1
10 8 6 3 1
10 8 6 4 1
10 8 6 5 1
10 8 6 7 1
10 8 6 9 1
10 8 7 1 2
10 8 7 2 1
10 8 7 3 1
10 8 7 4 1
10 8 7 5 1
10 8 7 6 1
10 8 7 9 1
10 8 9 1 2
10 8 9 2 1
10 8 9 3 1
10 8 9 4 1
10 8 9 5 1
10 8 9 6 1
10 8 9 7 1
10 9 1 2 3
10 9 1 3 2
10 9 1 4 2
10 9 1 5 2
10 9 1 6 2
10 9 1 7 2
10 9 1 8 2
10 9 2 1 3
10 9 2 3 1
10 9 2 4 1
10 9 2 5 1
10 9 2 6 1
10 9 2 7 1
10 9 2 8 1
10 9 3 1 2
10 9 3 2 1
10 9 3 4 1
10 9 3 5 1
10 9 3 6 1
10 9 3 7 1
10 9 3 8 1
10 9 4 1 2
10 9 4 2 1
10 9 4 3 1
10 9 4 5 1
10 9 4 6 1
10 9 4 7 1
10 9 4 8 1
10 9 5 1 2
10 9 5 2 1
10 9 5 3 1
10 9 5 4 1
10 9 5 6 1
10 9 5 7 1
10 9 5 8 1
10 9 6 1 2
10 9 6 2 1
10 9 6 3 1
10 9 6 4 1
10 9 6 5 1
10 9 6 7 1
10 9 6 8 1
10 9 7 1 2
10 9 7 2 1
10 9 7 3 1
10 9 7 4 1
10 9 7 5 1
10 9 7 6 1
10 9 7 8 1
10 9 8 1 2
10 9 8 2 1
10 9 8 3 1
10 9 8 4 1
10 9 8 5 1
10 9 8 6 1
10 9 8 7 1
共 5040 种排列!用时 18.17188 秒!

northwolves 2004-10-21
  • 打赏
  • 举报
回复
Sub pailie(ByVal m As Integer, ByVal n As Integer)
Dim starttime As Single, endtime As Single
Dim i As Integer, j As Integer, Num As Long
Dim a() As Integer '记载数组下标
Dim b() As Integer '记载使用标识
starttime = Timer '开始计时
ReDim a(1 To m)
ReDim b(1 To m)
i = 1
a(1) = 0
Do While i <= m
a(i) = a(i) + 1
If a(i) <= m Then
If b(a(i)) = 0 Then
If i = n Then '达到数组末,则输出组合情况
For j = 1 To n
Debug.Print a(j) & " "; '输出
Next
Debug.Print
Num = Num + 1
i = i - 1
b(a(i)) = 0 '清空使用标识
Else
b(a(i)) = 1 '标记已使用
i = i + 1
a(i) = 0 '重查
End If
End If
Else
i = i - 1
If i = 0 Then Exit Do '回溯至数组前则结束
b(a(i)) = 0
End If
DoEvents
Loop
endtime = Timer
Debug.Print "共 " & Num & " 种排列!用时 " & endtime - starttime & " 秒!"
End Sub

Private Sub Command1_Click()
pailie 10, 5
End Sub
victorycyz 2004-10-21
  • 打赏
  • 举报
回复
create table T ( s char)
insert T select '1' union select '2' union select '3' ...... union select 'a' union select 'b' ......

select a.n+b.n+c.n+d.n+e.n
from T a,T b,T c,T d,Te
where a.n<>b.n and a.n<>c.n and a.n<>d.n and a.n<>e.n
and b.n<>c.n and b.n<>d.n and b.n<>e.n
and c.n<>d.n and c.n<>e.n
and d.n<>e.n

drop table T
victorycyz 2004-10-21
  • 打赏
  • 举报
回复

又想了一下,上面只能正确处理个位数。如要处理两位的数字,要把字段换成char型的,10以后的数字用字母代替。
victorycyz 2004-10-21
  • 打赏
  • 举报
回复
用数据库,运算又快,写起来又简单:

create table T ( n int)
insert T select 1 union select 2 union select 3 ...... union select 10

select a.n*10000+b.n*1000+c.n*100+d.n*10+e.n
from T a,T b,T c,T d,Te
where a.n<>b.n and a.n<>c.n and a.n<>d.n and a.n<>e.n
and b.n<>c.n and b.n<>d.n and b.n<>e.n
and c.n<>d.n and c.n<>e.n
and d.n<>e.n

drop table T
chang5 2004-10-21
  • 打赏
  • 举报
回复
写好了,就是编译没过~!
chang5 2004-10-21
  • 打赏
  • 举报
回复
#include <list>
#include <iostream>
using namespace std;
list<int> L;
int m,n;

void select(int i=0,int c=0)
{
if(c==n){
list<int>::iterator *p;
for(p=L.begin();p!=L.end();p++)
cout<<*p<<'\t';
cout<<endl;
return; }
for(int x=i;x<m;x++)
{
L.push_back(x);
select(i+1,c+1);
L.pop_back();
}
}

void main()
{
while(cin>>m>>n)
select();
}
alenx3 2004-10-21
  • 打赏
  • 举报
回复
不考虑顺序

7,762

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧