1、VB上机等级考试常用算法VB等级考试常用算法1求因子数Option ExplicitPrivate Sub Command1_Click() Dim a As Integer, b() As Integer a = InputBox(请输入一个正整数) Call factor(a, b) For a = 1 To UBound(b) Print b(a); NextEnd SubPrivate Sub factor(x As Integer, fn() As Integer) Dim i As Integer, n As Integer ReDim fn(1) n = 1 fn(1) = 1
2、For i = 2 To x / 2 If x Mod i = 0 Then n = n + 1 ReDim Preserve fn(n) fn(n) = i End If Next iEnd Sub2求因子数之和Option ExplicitPrivate Sub Command1_Click() Dim a As Integer, b As Integer a = InputBox(请输入一个正整数) b = sum_factor(a) Print 因子之和为: Print b;End SubPrivate Function sum_factor(x As Integer) As Long
3、 Dim i As Integer, s As Integer s = 1 Print 1; For i = 2 To x / 2 If x Mod i = 0 Then s = s + i Print i; End If Next i Do While i x And xy yEnd FunctionPrivate Function lcm1(x As Integer, y As Integer) As Long Do lcm = lcm + x If lcm Mod x = 0 And lcm Mod y = 0 Then Exit Do LoopEnd Function6求阶乘Optio
4、n ExplicitPrivate Sub Command1_Click() Dim n As Integer, b As Integer n = InputBox(请输入一个正整数n:) Print n & !=; fact(n)End SubPrivate Function fact(x As Integer) As Long If x = 1 Then fact = 1 Else fact = x * fact(x - 1) End IfEnd FunctionPrivate Function fact1(x As Integer) As Long Dim i As Integer fa
5、ct1 = 1 For i = 2 To x fact1 = fact1 * i NextEnd Function7求分数约数Option ExplicitPrivate Sub Command1_Click() Dim a As Integer, b As Integer a = InputBox(请输入一个正整数a:) b = InputBox(请输入一个正整数b:) Print 分数 & a & / & b & 可以约分为:; divisor a, b Print a & / & bEnd SubPrivate Sub divisor(fz As Integer, fm As Integ
6、er) Dim min As Integer, i As Integer If fz a(j) Then t = a(i): a(i) = a(j): a(j) = t End If Next NextEnd SubPrivate Sub bub_sort(a() As Integer) Dim i As Integer, j As Integer, t As Integer, k As Integer For i = 1 To UBound(a) - 1 For j = 1 To UBound(a) - i If a(j) a(j + 1) Then t = a(j): a(j) = a(j
7、 + 1): a(j + 1) = t End If Next NextEnd Sub9判断一对数是否为互质数Option ExplicitPrivate Sub Command1_Click() Dim a As Integer, b As Integer a = InputBox(请输入第1个正整数) b = InputBox(请输入第2个正整数) Print a & 和 & b & 是:; If mutual_prime(a, b) Then Print 互质数! Else Print 不是互质数!End SubPrivate Function mutual_prime(x As Integer, y As Integer) As Boolean Dim i As Integer, min As Integer If x a(i + 1) Then E
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1