在VB中如何计算数字1~9的所有可能排列?
答案:5 悬赏:10 手机版
解决时间 2021-01-23 05:24
- 提问者网友:玫瑰园
- 2021-01-22 17:40
输出到TXT文件,求算法,谢谢
最佳答案
- 五星知识达人网友:渊鱼
- 2021-01-22 18:03
Private Sub Command1_Click()
Dim a(9) As Integer
n = CInt(InputBox("N="))
Open "d:\1234.txt" For Output As #1
k = 1
For i = 1 To n
a(i) = i
k = k * i
Next i
For i1 = 1 To k
For i = 1 To n
Print #1, a(i);
Next i
Print #1,
i = n - 1
Do While a(i) > a(i + 1)
i = i - 1
Loop
j = n
Do While a(j) < a(i)
j = j - 1
Loop
t = a(i): a(i) = a(j): a(j) = t
i = i + 1: j = n
Do While i < j
t = a(i): a(i) = a(j): a(j) = t
i = i + 1: j = j - 1
Loop
Next i1
Close #1
MsgBox "所有 " & k & " 个排列已经存入文件 d:\1234.txt,请查看!"
End Sub
以下是输入n=4时的内容:
1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
1 4 2 3
1 4 3 2
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 1 3
2 4 3 1
3 1 2 4
3 1 4 2
3 2 1 4
3 2 4 1
3 4 1 2
3 4 2 1
4 1 2 3
4 1 3 2
4 2 1 3
4 2 3 1
4 3 1 2
4 3 2 1
Dim a(9) As Integer
n = CInt(InputBox("N="))
Open "d:\1234.txt" For Output As #1
k = 1
For i = 1 To n
a(i) = i
k = k * i
Next i
For i1 = 1 To k
For i = 1 To n
Print #1, a(i);
Next i
Print #1,
i = n - 1
Do While a(i) > a(i + 1)
i = i - 1
Loop
j = n
Do While a(j) < a(i)
j = j - 1
Loop
t = a(i): a(i) = a(j): a(j) = t
i = i + 1: j = n
Do While i < j
t = a(i): a(i) = a(j): a(j) = t
i = i + 1: j = j - 1
Loop
Next i1
Close #1
MsgBox "所有 " & k & " 个排列已经存入文件 d:\1234.txt,请查看!"
End Sub
以下是输入n=4时的内容:
1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
1 4 2 3
1 4 3 2
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 1 3
2 4 3 1
3 1 2 4
3 1 4 2
3 2 1 4
3 2 4 1
3 4 1 2
3 4 2 1
4 1 2 3
4 1 3 2
4 2 1 3
4 2 3 1
4 3 1 2
4 3 2 1
全部回答
- 1楼网友:人類模型
- 2021-01-22 20:54
最简单的算法就是从1一直循环到987654321,逐个数检查是否有重复。
- 2楼网友:酒者煙囻
- 2021-01-22 20:01
如果位数是9位,且不能重复,那所有的可能:第一位有9种可能,第二位有9-1种,第三位有9-2种,以此类推,第9位位还剩一种可能。
9*8*7*6*5*4*3*2*1=362880
至于怎么写到vb里面,你再看看。我也写,写的好就回复,写不好,你只能自力更生了。
- 3楼网友:怙棘
- 2021-01-22 19:18
private sub command1_click()
dim a as string
for i = 1 to 35
for j = i + 1 to 36
for k = j + 1 to 37
for l = k + 1 to 38
for m = l + 1 to 39
for n = m + 1 to 40
if i + j + k + l + m + n >= 200 and i + j + k + l + m + n <= 250 then a = a & i & "," & j & "," & k & "," & l & "," & m & "," & n & vbcrlf
next n, m, l, k, j, i
text1.text = a
end sub
- 4楼网友:酒醒三更
- 2021-01-22 19:12
排列的个数也不限吗?比如:1、135、123456789都是要得出的结果吗?
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯