游戲中遇到這樣的問題,需要將一組已知的數(shù)據(jù)打亂,按照以前和現(xiàn)在的做法,總結(jié)了以下方法。
在做網(wǎng)站、網(wǎng)站設(shè)計(jì)中從網(wǎng)站色彩、結(jié)構(gòu)布局、欄目設(shè)置、關(guān)鍵詞群組等細(xì)微處著手,突出企業(yè)的產(chǎn)品/服務(wù)/品牌,幫助企業(yè)鎖定精準(zhǔn)用戶,提高在線咨詢和轉(zhuǎn)化,使成都網(wǎng)站營銷成為有效果、有回報(bào)的無錫營銷推廣。創(chuàng)新互聯(lián)公司專業(yè)成都網(wǎng)站建設(shè)十余年了,客戶滿意度97.8%,歡迎成都創(chuàng)新互聯(lián)客戶聯(lián)系。
方法一,最笨的菜鳥方法,也是容易想到的(幸好我沒想過這種方法 :))
從已知數(shù)組中隨機(jī)一個(gè)數(shù),然后加入到另一個(gè)數(shù)組中,在加入之前,先檢查是否已經(jīng)加入過。
這種方法有很大運(yùn)氣成分,且數(shù)據(jù)越大,效率越低,超過一定數(shù)目,則程序幾乎無法執(zhí)行,會(huì)一直卡在那里,代碼:
[java] view plain copy
package com.test;
import java.util.Random;
public class TestArray {
public static int runCount =0;//用于記錄方法運(yùn)算次數(shù)
看了你說遞歸的效率低。那么你可以不用的。
給出的方法就是先生成第一個(gè)排列,然后每次調(diào)用下面的函數(shù)給出下一個(gè)排列,這樣生成的效率很高,這個(gè)函數(shù)可以內(nèi)聯(lián)。
這個(gè)是很經(jīng)典的排列組合算法啊?在網(wǎng)上能搜到一大堆。
大概是那種帶指向的移動(dòng)的算法。我給你搜一個(gè)吧。
我找了幾個(gè),這個(gè)是我覺得說的比較清楚的,你可以仔細(xì)參考一下,看不懂的話再搜點(diǎn)別的好了。。
全排列的算法跟這個(gè)不太一樣的。需要有點(diǎn)改動(dòng)的。
至于語言的話,應(yīng)該不會(huì)有太大問題吧。。basic版的確實(shí)比較少,現(xiàn)在我也比較懶不想動(dòng)手寫。。還是要靠你自己啦。
★生成排列的算法:
比如要生成5,4,3,2,1的全排列,首先找出一個(gè)最小的排列12345, 然后依次調(diào)用n!次STL算法中的next_permutation()即可輸出所有的全排列情況。所以這種算法的細(xì)節(jié)就是STL algorithm中next_permutation()的實(shí)現(xiàn)機(jī)制。詳細(xì)的實(shí)現(xiàn)代碼,大伙可以參考侯捷的《STL源代碼剖析》,在這里我只說一下我的理解:
1 首先從最尾端開始往前尋找兩個(gè)相鄰元素,令第一個(gè)元素為*i,第二個(gè)元素為*ii,且滿足*i*ii,找到這樣一組相鄰的元素后。
2 再從最尾端開始往前檢驗(yàn),找出第一個(gè)大于*i的元素,令為*k,將i,k元素對(duì)調(diào)。
3 再將ii及ii之后的所有元素顛倒排列,此即所求之"下一個(gè)"排列。
prev_permutation()算法的思路也基本相同,只不過它們尋找的"拐點(diǎn)"不同,在next_permutation()算法中尋找的是峰值拐點(diǎn),而在prev_permutation()算法中尋找的是谷值拐點(diǎn)。另外,在第二步中,prev_permutation()要找的是第一個(gè)小于*i的元素而不是第一個(gè)大于*i的元素。
具體例子,有空再舉,現(xiàn)在時(shí)間太晚了:)
★生成組合的算法:
如下面截圖所示,分全組合和r-組合兩種情況。
這里有一段核心代碼:
//--------------------------------------------------------
// Generate next combination (algorithm from Rosen p. 286)
//--------------------------------------------------------
public int[] getNext () {
if (numLeft.equals (total)) {
numLeft = numLeft.subtract (BigInteger.ONE);
return a;
}
int i = r - 1;
while (a[i] == n - r + i) {
i--;
}
a[i] = a[i] + 1;
for (int j = i + 1; j r; j++) {
a[j] = a[i] + j - i;
}
numLeft = numLeft.subtract (BigInteger.ONE);
return a; //這里返回的a數(shù)組,存儲(chǔ)的就是下標(biāo)的排列組合。
}
到這里,也許大伙會(huì)有一個(gè)疑問,假如要求的不是數(shù)字的排列組合,而是字符或字符串的排列組合呢?怎么辦?其實(shí)很簡單,你只要拿數(shù)組的下標(biāo)來做排列組合,返回他們下標(biāo)的排列組合,然后再到原數(shù)組中讀取字符串值,就可以輸出全部的排列組合結(jié)果。
試試看:
For?i?=?LBound(moto)?To?UBound(moto)?-?1
For?j?=?LBound(moto)?To?UBound(moto)?-?1?-?i
If?moto(j)??moto(j?+?1)?Then
t?=?moto(j)
moto(j)?=?moto(j?+?1)
moto(j?+?1)?=?t
End?If
Next?j
Next?i
For?i?=?LBound(moto)?To?UBound(moto)
Print?moto(i);
Next?i
建議用 DataGridView(你用的是它吧?)內(nèi)建的排序方法來排序。介紹和示例代碼可以參考MSDN:
如果你是從vb6剛過渡上vb。net,建議還是用冒泡排序法,容易理解。
如果你正努力學(xué)習(xí)vb。net的方法,推薦一個(gè)例子如下:
Imports System
Imports System.Collections
Public Class SamplesArray
Public Class myReverserClass
Implements IComparer
' Calls CaseInsensitiveComparer.Compare with the parameters reversed.
Function Compare(x As Object, y As Object) As Integer _
Implements IComparer.Compare
Return New CaseInsensitiveComparer().Compare(y, x)
End Function 'IComparer.Compare
End Class 'myReverserClass
Public Shared Sub Main()
' Creates and initializes a new Array and a new custom comparer.
Dim myArr As [String]() = {"The", "QUICK", "BROWN", "FOX", "jumps", "over", "the", "lazy", "dog"}
Dim myComparer = New myReverserClass()
' Displays the values of the Array.
Console.WriteLine("The Array initially contains the following values:")
PrintIndexAndValues(myArr)
' Sorts a section of the Array using the default comparer.
Array.Sort(myArr, 1, 3)
Console.WriteLine("After sorting a section of the Array using the default comparer:")
PrintIndexAndValues(myArr)
' Sorts a section of the Array using the reverse case-insensitive comparer.
Array.Sort(myArr, 1, 3, myComparer)
Console.WriteLine("After sorting a section of the Array using the reverse case-insensitive comparer:")
PrintIndexAndValues(myArr)
' Sorts the entire Array using the default comparer.
Array.Sort(myArr)
Console.WriteLine("After sorting the entire Array using the default comparer:")
PrintIndexAndValues(myArr)
' Sorts the entire Array using the reverse case-insensitive comparer.
Array.Sort(myArr, myComparer)
Console.WriteLine("After sorting the entire Array using the reverse case-insensitive comparer:")
PrintIndexAndValues(myArr)
End Sub 'Main
Public Shared Sub PrintIndexAndValues(myArr() As [String])
Dim i As Integer
For i = 0 To myArr.Length - 1
Console.WriteLine(" [{0}] : {1}", i, myArr(i))
Next i
Console.WriteLine()
End Sub 'PrintIndexAndValues
End Class 'SamplesArray
'This code produces the following output.
'
'The Array initially contains the following values:
' [0] : The
' [1] : QUICK
' [2] : BROWN
' [3] : FOX
' [4] : jumps
' [5] : over
' [6] : the
' [7] : lazy
' [8] : dog
'
'After sorting a section of the Array using the default comparer:
' [0] : The
' [1] : BROWN
' [2] : FOX
' [3] : QUICK
' [4] : jumps
' [5] : over
' [6] : the
' [7] : lazy
' [8] : dog
'
'After sorting a section of the Array using the reverse case-insensitive comparer:
' [0] : The
' [1] : QUICK
' [2] : FOX
' [3] : BROWN
' [4] : jumps
' [5] : over
' [6] : the
' [7] : lazy
' [8] : dog
'
'After sorting the entire Array using the default comparer:
' [0] : BROWN
' [1] : dog
' [2] : FOX
' [3] : jumps
' [4] : lazy
' [5] : over
' [6] : QUICK
' [7] : the
' [8] : The
'
'After sorting the entire Array using the reverse case-insensitive comparer:
' [0] : the
' [1] : The
' [2] : QUICK
' [3] : over
' [4] : lazy
' [5] : jumps
' [6] : FOX
' [7] : dog
' [8] : BROWN
加了單引號(hào)就是一個(gè)常量字符串了,對(duì)于每一行都是一樣的
像這種放在最前面的字段,order by 1 就可以了