VB基本算法

更新时间:2024-02-27 11:50:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

1、判断一个数是不是素数

Private Sub Form_Click()

num = Val(InputBox(\请输入整数num\For i = 2 To num-1

If num Mod i = 0 Then

Print \整数\不是素数\ Exit For End If Next i

If i > num-1 Then Print \整数\是素数\End Sub

2、1~100内素数之和

Private Sub Form_Click() Dim sum% sum = 1

For i = 2 To 100

For j = 2 To i-1

If i Mod j = 0 Then Exit For End If Next j

If j >i-1Then sum = sum + i: Print i Next i

Print \以内的素数之和为:\End Sub

3、杨辉三角

Option Base 1

Private Sub Form_Click() Dim a(10, 10)

For i = 1 To 10 a(i, 1) = 1 a(i, i) = 1 Next i

For i = 3 To 10

For j = 2 To i - 1

a(i, j) = a(i - 1, j - 1) + a(i - 1, j) Next j

Next i

For i = 1 To 10 For j = 1 To i

Print a(i, j); \ Next j Print Next i

End Sub

?111?1????...3574、求数列4精确到最后一项的绝对值小

于0.0001

Private Sub Form_Click()

Dim a!, sn! n% = 1 Do

a = 1 / (2 * n - 1) a = a * (-1) ^ (n - 1) sn = sn + a n = n + 1

Loop Until Abs(a) < 0.0001 sn = sn * 4 Print sn End Sub

5、数组输入输出

Option Base 1 Dim a()

Private Sub Form_Click()

n% = Val(InputBox(\请输入第一维的上界\m% = Val(InputBox(\请输入第二维的上界\ReDim a(n, m) Randomize For i = 1 To n

For j = 1 To m

a(i, j) = Int(Rnd * 100) '或者 a(i,j) = Val(InputBox(\请输入数组元素\ Next j Next i

For i = 1 To n

For j = 1 To m

Print a(i, j); \ \ Next j Print Next i

End Sub

6、数组的转置

Private Sub Form_Click() Dim a%(3, 4), b%(4, 3) Print \转置前:\For i = 1 To 3 For j = 1 To 4

a(i, j) = Int(Rnd * 100) Print a(i, j); \ \ Next j Print Next i

For i = 1 To 4 For j = 1 To 3 b(i, j) = a(j, i) Next j Next i Print

Print \转置后:\For i = 1 To 4 For j = 1 To 3

Print b(i, j); \ \ Next j Print Next i

End Sub

7、数组的插入

Option Base 1 Dim a()

'或者 a(i,j) = Val(InputBox(\请输入数组元素\ Private Sub Form_Click()

n% = Val(InputBox(\请输入第一维的上界\ReDim a(n)

Print \插入前:\For i = 1 To n

a(i) = Int(Rnd * 100) '或者 a(i,j) = Val(InputBox(\请输入数组元素\ Print a(i); \ \Next i

col% = Val(InputBox(\请输入要插入的位置\num = Val(InputBox(\请输入要插入的数\ReDim Preserve a(n + 1)

For i = n + 1 To col + 1 Step -1 a(i) = a(i - 1) Next i

a(col) = num Print

Print \插入后:\For i = 1 To n + 1

Print a(i); \ \Next i

End Sub

8、数组元素的删除

Option Base 1 Dim a()

Private Sub Form_Click()

n% = Val(InputBox(\请输入第一维的上界\ReDim a(n)

Print \删除前:\For i = 1 To n

a(i) = Int(Rnd * 100) '或者 a(i,j) = Val(InputBox(\请输入数组元素\ Print a(i); \ \Next i

col% = Val(InputBox(\请输入要删除的位置\num = a(col)

For i = col To n - 1

a(i) = a(i + 1) Next i a(i) = 0 Print

Print \删除后:\For i = 1 To n

Print a(i); \ \Next i

End Sub

9、找一列数中最大(最小)的一个数及其下标

Option Base 1 Dim a()

Private Sub Form_Click()

n% = Val(InputBox(\请输入第一维的上界\ReDim a(n)

Print \原数列:\For i = 1 To n

a(i) = Int(Rnd * 100) '或者 a(i,j) = Val(InputBox(\请输入数组元素\ Print a(i); \ \Next i

Max = a(1) col = 1

For i = 2 To n

If Max < a(i) Then Max = a(i) col = i End If Next i Print

Print \最大的数是:\下标为:\

End Sub

10 冒泡排序

对数组a()进行冒泡排序

Option Base 1

Private Sub Form_Click() Dim a%(10) Randomize For i = 1 To 10

a(i) = Int(Rnd * 100) Next i

Print \排序前:\Print

For i = 1 To 10 Print a(i); \Next i Print

For i = 1 To 9 '冒泡排序 For j = 1 To 10 - i

If a(j) > a(j + 1) Then t = a(j)

a(j) = a(j + 1) a(j + 1) = t End If Next j Next i

Print \排序后\Print

For i = 1 To 10 Print a(i); \Next i End Sub

11、数列的逆置

Option Base 1

Private Sub Form_Click() Dim a%(10) Randomize For i = 1 To 10

a(i) = Int(Rnd * 100) Next i

Print \逆置前:\Print

For i = 1 To 10 Print a(i); \Next i Print

For i = 1 To 10 / 2 '逆置 t = a(i)

a(i) = a(10 - i + 1) a(10 - i + 1) = t Next i

Print \逆置后\Print

For i = 1 To 10 Print a(i); \Next i End Sub

12、用数组以及传递至的方式进行参数传递

编写过程,实现查找一个数列中最大的元素和它所在的位置 Option Base 1 Dim a()

Private Sub f(b%(), max%, col%) max = b(1) col = 1

For i = 2 To UBound(b, 1) If max < b(i) Then max = b(i) col = i End If Next i End Sub

Private Sub Form_Click() Dim column%, max% Dim a%(10) Randomize

Print \原数列:\For i = 1 To 10

a(i) = Int(Rnd * 100) '或者 a(i,j) = Val(InputBox(\请输入数组元素\ Print a(i); \ \

Next i

Call f(a(), max, column) Print

Print \最大的数是:\ \下标为:\

End Sub

13、拆分一个整数

Private Sub Form_Click()

num% = Val(InputBox(\请输入待拆分的整数\Print \待拆分的数是\Print

While num <> 0

x = num Mod 10 Print x; \ num = num / 10 Wend End Sub

14、拆分一个整数(递归)

Private Sub f(x%) If x < 10 Then Print x; \ \Else

Print x Mod 10; \ \ 'a% = Int(x / 10)

f (x \\ 10) '或者f(a) End If End Sub

Private Sub Form_Click()

num% = Val(InputBox(\请输入待拆分的整数\Print \待拆分的数是\Print

f num

End Sub

15、阶乘

循环法: For i=1 to n s=s* next i

递归法

Function f(n%) if n=1 then

f=1 else

f=n*f(n-1) End if

End Function

16、求最大公约数

1、循环法

Private Function f(ByVal x%, ByVal y%) Dim r%

If x < y Then t = x x = y y = t End If

While y <> 0 r = x Mod y x = y y = r Wend f = x

End Function

Private Sub Form_Click()

m% = Val(InputBox(\第一个数是\n% = Val(InputBox(\第二个数是\Print \输入的两个数是\Print

r = f(m, n)

Print \最大公约数是\ \最小公倍数是\End Sub

2、递归法

Private Function f(ByVal x%, ByVal y%) Dim r% r = x Mod y If r = 0 Then f = y Else

x = y y = r f = f(x, y) End If

End Function

Private Sub Form_Click()

m% = Val(InputBox(\第一个数是\n% = Val(InputBox(\第二个数是\Print \输入的两个数是\Print

r = f(m, n)

Print \最大公约数是\ \最小公倍数是\End Sub

本文来源:https://www.bwwdw.com/article/hkqa.html

Top