VB全排列算法。
发布网友
发布时间:2022-07-26 23:20
我来回答
共4个回答
热心网友
时间:2023-10-23 07:59
新建一个工程,在窗体上新建一个text1,一个command1,把以下代码复制到工程中运行试试
Option Explicit
Private List() As String
Public index&, tmp$
Private Sub Command1_Click()
ReDim List(Len(Trim(Form1.Text1.Text)))
For index = 1 To Len(Trim(Form1.Text1.Text))
List(index) = Mid(Form1.Text1.Text, index, 1)
Next index
Perm List, 1, Len(Trim(Form1.Text1.Text))
End Sub
Private Sub Form_Load()
Form1.AutoRedraw = True
End Sub
Public Function Swap(ByRef Num1 As String, ByRef Num2 As String) '交换两个数
tmp = Num1
Num1 = Num2
Num2 = tmp
End Function
Public Function Perm(ByRef ListTar() As String, ByVal k As Long, ByVal m As Long) '全排列函数
Dim i&
If k > m Then
For i = 1 To m
Print ListTar(i);
Next i
Print
Else
For i = k To m
Swap ListTar(k), ListTar(i)
Perm ListTar, k + 1, m
Swap ListTar(k), ListTar(i)
Next i
End If
End Function
运行效果
参考资料:邓向阳,万婷婷.算法分析与设计.冶金工业出版社.2005
热心网友
时间:2023-10-23 07:59
'添加一个按钮,将下列代码复制到代码窗口
Private Sub Command1_Click()
Open "c:\9阶乘种.txt" For Output As #1
Close #1
For i1 = 1 To 9
For i2 = 1 To 9
If i2 = i1 Then GoTo G2
For i3 = 1 To 9
If i3 = i1 Or i3 = i2 Then GoTo G3
For i4 = 1 To 9
If i4 = i1 Or i4 = i2 Or i4 = i3 Then GoTo G4
For i5 = 1 To 9
If i5 = i1 Or i5 = i2 Or i5 = i3 Or i5 = i4 Then GoTo G5
For i6 = 1 To 9
If i6 = i1 Or i6 = i2 Or i6 = i3 Or i6 = i4 Or i6 = i5 Then GoTo G6
For i7 = 1 To 9
If i7 = i1 Or i7 = i2 Or i7 = i3 Or i7 = i4 Or i7 = i5 Or i7 = i6 Then GoTo G7
For i8 = 1 To 9
If i8 = i1 Or i8 = i2 Or i8 = i3 Or i8 = i4 Or i8 = i5 Or i8 = i6 Or i8 = i7 Then GoTo G8
For i9 = 1 To 9
If i9 = i1 Or i9 = i2 Or i9 = i3 Or i9 = i4 Or i9 = i5 Or i9 = i6 Or i9 = i7 Or i9 = i8 Then GoTo G9
DoEvents
Open "c:\9阶乘种.txt" For Append As #1
k = i1 & i2 & i3 & i4 & i5 & i6 & i7 & i8 & i9
Print #1, k
Label1 = k
Close #1
G9:
Next i9
G8:
Next i8
G7:
Next i7
G6:
Next i6
G5:
Next i5
G4:
Next i4
G3:
Next i3
G2:
Next i2
G1:
Next i1
MsgBox "完成排列,请擦看!"
Shell "notepad.exe c:\9阶乘种.txt ", 1
End Sub
热心网友
时间:2023-10-23 08:00
曾做过一道题,看算法。
题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
1.程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去掉不满足条件的排列。
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
enum color{one,two,three,four};
color pri;
int i,j,k,n=0,loop;
for (i=one;i<=four;i++)
for(j=one;j<=four;j++)
if(i!=j)
{ for (k=one;k<=black;k++)
if((k!=i)&&(k!=j))
{n=n+1;
cout<<setw(3)<<n;
for(loop=1;loop<=3;loop++)
{switch(loop)
{case 1:pri=color(i);break;
case 2:pri=color(j);break;
case 3:pri=color(k);break;
default:break;
}
switch(pri)
{case one:cout<<setw(8)<<"one";break;
case two:cout<<setw(8)<<"two";break;
case three:cout<<setw(8)<<"three";break;
case four:cout<<setw(8)<<"four";break;
default:break;
}
}
cout<<endl;
}
}
cout<<"total:"<<n<<endl;
return 0;
}
热心网友
时间:2023-10-23 08:00
那个,怎样才算好?
private sub form_click()
dim level as long
dim num() as long
dim i as long
level=9
redim num(1 to level)
for i=1 to level
num(i)=i
next i
call printnum(num(),level,level)
end sub
private sub printnum(num() as long,level as long,total as long)
dim i as long,temp as long
if level=1 then
for i=1 to total
print num(i);
next i
print
else
call printnum(num(),level-1,total)
for i=1 to level-1
temp=num(level)
num(level)=num(level-1)
num(level-1)=temp
call printnum(num(),level-1,total)
next i
end if
end sub