游戲中遇到這樣的問題,需要將一組已知的數(shù)據(jù)打亂,按照以前和現(xiàn)在的做法,總結(jié)了以下方法。

在做網(wǎng)站、網(wǎng)站設(shè)計中從網(wǎng)站色彩、結(jié)構(gòu)布局、欄目設(shè)置、關(guān)鍵詞群組等細微處著手,突出企業(yè)的產(chǎn)品/服務(wù)/品牌,幫助企業(yè)鎖定精準用戶,提高在線咨詢和轉(zhuǎn)化,使成都網(wǎng)站營銷成為有效果、有回報的無錫營銷推廣。創(chuàng)新互聯(lián)公司專業(yè)成都網(wǎng)站建設(shè)十余年了,客戶滿意度97.8%,歡迎成都創(chuàng)新互聯(lián)客戶聯(lián)系。
方法一,最笨的菜鳥方法,也是容易想到的(幸好我沒想過這種方法 :))
從已知數(shù)組中隨機一個數(shù),然后加入到另一個數(shù)組中,在加入之前,先檢查是否已經(jīng)加入過。
這種方法有很大運氣成分,且數(shù)據(jù)越大,效率越低,超過一定數(shù)目,則程序幾乎無法執(zhí)行,會一直卡在那里,代碼:
[java] view plain copy
package com.test;
import java.util.Random;
public class TestArray {
public static int runCount =0;//用于記錄方法運算次數(shù)
看了你說遞歸的效率低。那么你可以不用的。
給出的方法就是先生成第一個排列,然后每次調(diào)用下面的函數(shù)給出下一個排列,這樣生成的效率很高,這個函數(shù)可以內(nèi)聯(lián)。
這個是很經(jīng)典的排列組合算法啊?在網(wǎng)上能搜到一大堆。
大概是那種帶指向的移動的算法。我給你搜一個吧。
我找了幾個,這個是我覺得說的比較清楚的,你可以仔細參考一下,看不懂的話再搜點別的好了。。
全排列的算法跟這個不太一樣的。需要有點改動的。
至于語言的話,應(yīng)該不會有太大問題吧。。basic版的確實比較少,現(xiàn)在我也比較懶不想動手寫。。還是要靠你自己啦。
★生成排列的算法:
比如要生成5,4,3,2,1的全排列,首先找出一個最小的排列12345, 然后依次調(diào)用n!次STL算法中的next_permutation()即可輸出所有的全排列情況。所以這種算法的細節(jié)就是STL algorithm中next_permutation()的實現(xiàn)機制。詳細的實現(xiàn)代碼,大伙可以參考侯捷的《STL源代碼剖析》,在這里我只說一下我的理解:
1 首先從最尾端開始往前尋找兩個相鄰元素,令第一個元素為*i,第二個元素為*ii,且滿足*i*ii,找到這樣一組相鄰的元素后。
2 再從最尾端開始往前檢驗,找出第一個大于*i的元素,令為*k,將i,k元素對調(diào)。
3 再將ii及ii之后的所有元素顛倒排列,此即所求之"下一個"排列。
prev_permutation()算法的思路也基本相同,只不過它們尋找的"拐點"不同,在next_permutation()算法中尋找的是峰值拐點,而在prev_permutation()算法中尋找的是谷值拐點。另外,在第二步中,prev_permutation()要找的是第一個小于*i的元素而不是第一個大于*i的元素。
具體例子,有空再舉,現(xiàn)在時間太晚了:)
★生成組合的算法:
如下面截圖所示,分全組合和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ù)組,存儲的就是下標的排列組合。
}
到這里,也許大伙會有一個疑問,假如要求的不是數(shù)字的排列組合,而是字符或字符串的排列組合呢?怎么辦?其實很簡單,你只要拿數(shù)組的下標來做排列組合,返回他們下標的排列組合,然后再到原數(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,建議還是用冒泡排序法,容易理解。
如果你正努力學習vb。net的方法,推薦一個例子如下:
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
加了單引號就是一個常量字符串了,對于每一行都是一樣的
像這種放在最前面的字段,order by 1 就可以了
網(wǎng)站標題:vb.net的排序法,vbnet數(shù)組排序方法
URL分享:http://chinadenli.net/article13/dsggegs.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、定制網(wǎng)站、App設(shè)計、搜索引擎優(yōu)化、電子商務(wù)、網(wǎng)站設(shè)計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)