制作一个尝试所有排列的循环 [英] Making a loop that tries all permutations

查看:98
本文介绍了制作一个尝试所有排列的循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从A到D循环的代码,并在循环继续时在文本框中显示结果。



I have a code that loops from A to D and displays the result in a text box as the loop continues.

Dim test(0 To 3) As String

test(0) = "A"
test(1) = "B"
test(2) = "C"
test(3) = "D"

For i As Int32 = 0 To test.Length - 1
TextBox4.Text = test(i)
TextBox4.Refresh()
Me.Refresh()
Next





但问题是循环只有A,B,C,D

怎么能我让循环尝试所有排列,所以它是:

A

B

C

D

AA

AB

AC ....依此类推?



But the problem is that the loop only goes like A , B , C ,D
How can I make the loop try all permutations so it's :
A
B
C
D
AA
AB
AC ....and so on?

推荐答案

此CodeProject文章 - 使用C#泛型的排列,组合和变体 [ ^ ]应该给你所有你需要的如何这样做。



它出现在C#但有免费在线网站 [ ^ 如果你不能自己做,那将转换为vb.net
This CodeProject article - Permutations, Combinations, and Variations using C# Generics[^] should give you all you need as to the "how" of doing it.

It's presented in C# but there are free on-line sites[^] that will convert to vb.net if you can't do it yourself


鉴于你的答案太微不足道,但它不会帮助你学习。

除了编程之外,如果必须在没有计算机的情况下手动解决它会怎么做:

Given you the answer is too trivial but it is not going to help you to learn.
Programming aside, what would you do if you have to solve it manually without computer:
Let n starts from 0, i.e. n = 0, loop
1.   Pick out the nth letter in the test array
     1.1.  m = n + 1, loop
     1.2.  pick out the mth letter in the test array, concatenate with the nth letter
     1.3.  If there is any letter after mth, then 
               1.3.1.  m = m + 1
               1.3.2.  Goto 1.2
           else
               1.3.3.  n = n +1
               1.3.4   goto 1



我看到你可以使第一个for循环工作,现在将上面的内容翻译成另一个for循环。毕竟,这是你的功课。


I see that you can make the first for loop worked, now translate the above into another for loop. After all, that is your homework.


这篇关于制作一个尝试所有排列的循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆