网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

vb.net排列,vbnet数组排序方法

vb.net 排列组合 代码

第一题:

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:空间域名、网站空间、营销软件、网站建设、响水网站维护、网站推广。

不需要任何控件,代码如下:

Private Sub Form_Click()

Dim A() As Integer, N As Integer

Dim St As String, I As Integer, J As Integer

Randomize

Do

St = InputBox("数字的个数", "输入", Int(Rnd * 100))

If St = "" Then

MsgBox "请输入数字!"

Else

N = Int(Val(St))

If N 1 Then

MsgBox "请输入大于0的数字!"

Else

Exit Do

End If

End If

Loop

ReDim A(N)

For I = 1 To N

Do

St = InputBox("第" + Str(I) + "个数字", "输入", Int(Rnd * 100))

If St = "" Then

MsgBox "请输入数字!"

Else

A(I) = Int(Val(St))

Exit Do

End If

Loop

Next

For I = 1 To N - 1

For J = I + 1 To N

If A(I) A(J) Then

A(0) = A(I)

A(I) = A(J)

A(J) = A(0)

End If

Next

Next

For I = 1 To N

Open App.Path "\" Trim(Str(I)) ".txt" For Output As #1

Print #1, A(I)

Close #1

Next

Print "已经把"; N; "个数写入到"; App.Path; "\1.txt 到 "; N; ".txt中.请查看."

End Sub

'已经运行过.

第二题:

DIM 是变量声明语句,它的格式为:

dim 变量名[as 格式] [,变量名[as 格式][,变量名[as 格式]......]

其中:

变量名:以字母或汉字开始的字串,代表一个变量

格式有以下几种:

属于数字的有五种:

(1)字节型:byte可取值0-255

(2)整形:integer可取值-32768至32767

(3)长整形:long(可取值范围很大的正负整数)

(4)单精度型:single(可取值小数)

(5)双精度型:double(可取值范围更大,小数位数更多的小数)

字符串型:string(可代表由字母\数字或汉字组成的字符集合)

布尔型:boolean(取值为ture\false)

日期型:date(可表示形如2009-5-26 02:36这样的组合)

如果要用姓名\住址\单位名称...等用字符串型(string)

eg:dim name as string(用name变量表示名字时,声明成字符串变量)

如果是用数字需要做计算,如工资\合计\人数....等要用数字型,但有一个原则,优先选用范围小的(按照字节型(byte)\整形(integer)\长整形(long)\单精度型(single)\双精度型(double)的顺序选择),够用就可以了,这样可以占用内存少,运算速度快.

eg:dim count as integer(用integer表示员工人数时,可声明成整形变量)

eg:dim sum as single(用sum表示工资时,可声明成单精度型变量)

不知是否说得清楚了.

vb.net 排列组合算法

看了你说递归的效率低。那么你可以不用的。

给出的方法就是先生成第一个排列,然后每次调用下面的函数给出下一个排列,这样生成的效率很高,这个函数可以内联。

这个是很经典的排列组合算法啊?在网上能搜到一大堆。

大概是那种带指向的移动的算法。我给你搜一个吧。

我找了几个,这个是我觉得说的比较清楚的,你可以仔细参考一下,看不懂的话再搜点别的好了。。

全排列的算法跟这个不太一样的。需要有点改动的。

至于语言的话,应该不会有太大问题吧。。basic版的确实比较少,现在我也比较懒不想动手写。。还是要靠你自己啦。

★生成排列的算法:

比如要生成5,4,3,2,1的全排列,首先找出一个最小的排列12345, 然后依次调用n!次STL算法中的next_permutation()即可输出所有的全排列情况。所以这种算法的细节就是STL algorithm中next_permutation()的实现机制。详细的实现代码,大伙可以参考侯捷的《STL源代码剖析》,在这里我只说一下我的理解:

1 首先从最尾端开始往前寻找两个相邻元素,令第一个元素为*i,第二个元素为*ii,且满足*i*ii,找到这样一组相邻的元素后。

2 再从最尾端开始往前检验,找出第一个大于*i的元素,令为*k,将i,k元素对调。

3 再将ii及ii之后的所有元素颠倒排列,此即所求之"下一个"排列。

prev_permutation()算法的思路也基本相同,只不过它们寻找的"拐点"不同,在next_permutation()算法中寻找的是峰值拐点,而在prev_permutation()算法中寻找的是谷值拐点。另外,在第二步中,prev_permutation()要找的是第一个小于*i的元素而不是第一个大于*i的元素。

具体例子,有空再举,现在时间太晚了:)

★生成组合的算法:

如下面截图所示,分全组合和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数组,存储的就是下标的排列组合。

}

到这里,也许大伙会有一个疑问,假如要求的不是数字的排列组合,而是字符或字符串的排列组合呢?怎么办?其实很简单,你只要拿数组的下标来做排列组合,返回他们下标的排列组合,然后再到原数组中读取字符串值,就可以输出全部的排列组合结果。

vb.net 如何让动态生成的90个Label 排列成 3列 30行?

需要一点,数学知识:画个图,容易理解:

如图分析:

第1行第1列控件Label1(0)的Left和Top;

Label1(0).Left = L

Label1(0).Top = T

Label1(0).Width = 宽

Label1(0).Height = 高

并设控件之间的行距,列举分别为:V,H

那么:

        第1列                                 第2列                                     第3列

第1行    Label1(0).Left = L               Label1(1).Left = L + 宽 + H          Label1(2).Left = L + 2 * (宽 + H)

   Label1(0).Top = T                Label1(1).Top = T                    Label1(2).Top = T

第2行    Label1(3).Left = L               Label1(4).Left = L + 宽 + H          Label1(5).Left = L + 2 * (宽 + H)

   Label1(3).Top = T + 高 + V       Label1(4).Top = T + 高 + V           Label1(5).Top = T + 高 + V

第3行    Label1(6).Left = L               Label1(7).Left = L + 宽 + H          Label1(8).Left = L + 2 * (宽 + H)

   Label1(6).Top = T + 2*(高 + V)   Label1(7).Top = T + 2*(高 + V)       Label1(8).Top = T + 2*(高 + V)

第4行    Label1(9).Left = L               Label1(10).Left = L + 宽 + H         Label1(11).Left = L + 2 * (宽 + H)

   Label1(9).Top = T + 3*(高 + V)   Label1(10).Top = T + 3*(高 + V)      Label1(11).Top = T + 3*(高 + V)

。。。。。。

所以:

下标为 i 的控件的 Left、Top属性为?:

确定其列号的方法:(i Mod 3) + 1,其Left属性为:Label1(i).Left = L + (i Mod 3) * (宽 + H)

确定其行号的方法:(i \ 3) + 1,其Top属性为:Label1(i).Top = T + (i \ 3) * (高 + V)

VB.net 数组怎么按任意元素的顺序排序输出

你直接传一个数组进去,而且是一个结构体数组,array.sort怎么知道根据结构中的哪一个属性进行排序?放一个c#的代码你看看,VB和C#很相似的

class Program

{

static void Main(string[] args)

{

People[] p = new People[3]

{

new People{name="张三"},

new People{name="李四"},

new People{name="张二名"}

};

//重点传一个实现了IComparer接口的类进去,告诉Array.Sort怎么排序

Array.Sort(p, new PeopleCompare());

foreach (var item in p)

{

Console.WriteLine(item.name);

}

Console.ReadKey();

}

}

//People结构体,换成类一样的

public struct People

{

public string name { get; set; }

}

//实现了IComparer接口的类

public class PeopleCompare : IComparer

{

public int Compare(object x, object y)

{

People p1 = (People)x ;

People p2 = (People)y;

return p1.name.CompareTo(p2.name);

}

}

VB.NET数组的排序法?

如果你是从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


分享标题:vb.net排列,vbnet数组排序方法
网页URL:http://bjjierui.cn/article/heosij.html

其他资讯