'Q1:己知正整數 n ,求 1+2+3+...+ n 之和
Dim n As Integer
n = InputBox("n = ", "Q1:己知正整數 n ,求 1+2+3+...+ n 之和", 1)
Me.TextBox1.Text = "1 + " & "... +" & n & " = " & sum(n)
'Q1:sumOdd(n)
'奇數
Dim n As Integer
n = InputBox("n = ", "Q1:sumOdd(n)", 1)
n = If(n Mod 2 = 0, n - 1, n)
Me.TextBox2.Text = "從 1 " & "加奇數到 " & n & " = " & sumOdd(n)
'Q1:sumEven(n)
'偶數
Dim n As Integer
n = InputBox("n = ", "Q1:sumEven(n)", 1)
n = If(n Mod 2 = 0, n, n - 1)
Me.TextBox3.Text = "從 1 " & "加偶數到 " & n & " = " & sumEven(n)
'Q2:己知正整數 n ,求 n!
Dim n As Integer
n = InputBox("n = ", "Q2:己知正整數 n ,求 n!", 1)
Me.TextBox4.Text = "1 x " & " ... x " & n & " = " & fac(n)
'Q3: 求兩個正整數 a, b 的最大公因數
Dim a, b As Integer
a = InputBox("a = ", "Q3: 求兩個正整數 a, b 的最大公因數", 1)
b = InputBox("b = ", "Q3: 求兩個正整數 a, b 的最大公因數", 1)
Me.TextBox5.Text = a & " 和 " & b & " 的最大公因數 = " & GCD(a, b)
'Q3: lcm
Dim a, b As Integer
a = InputBox("a = ", "Q3: 求兩個正整數 a, b 的最小公倍數", 1)
b = InputBox("b = ", "Q3: 求兩個正整數 a, b 的最小公倍數", 1)
Me.TextBox6.Text = a & " 和 " & b & " 的最小公倍數 = " & lcm(a, b)
'Q4: 月兔問題 與 費氏數列
Dim n As Integer
Dim ans = ""
n = InputBox("n = ", "Q4: 月兔問題 與 費氏數列", 1)
For i = 0 To n
ans &= Fib(i) & If(i <> n, ",", "")
Next
Me.TextBox7.Text = "總對數費氏數列:" & ans
'Q5: 十進位數值轉二進位字串
Dim n As Integer
n = InputBox("n = ", "Q5: 十進位數值轉二進位字串", 0)
Me.TextBox8.Text = n & " 從十進位轉二進位 = " & "(" & Bin(n) & ")" & "2"
'Q5: Quad(n)
Dim n As Integer
n = InputBox("n = ", "Q5: Quad(n)", 0)
Me.TextBox9.Text = n & " 從十進位轉四進位 = " & "(" & Quad(n) & ")" & "4"
Module _1110534013
'Q1:己知正整數 n ,求 1+2+3+...+ n 之和
'recursive 遞迴函數
Function sum(n)
If n <= 1 Then
Return n
Else
Return sum(n - 1) + n
End If
End Function
'Q1:sumOdd(n)
Function sumOdd(n)
If n <= 1 Then
Return n
Else
Return sumOdd(n - 2) + n
End If
End Function
'Q1:sumEven(n)
Function sumEven(n)
If n <= 1 Then
Return n
Else
Return sumEven(n - 2) + n
End If
End Function
'Q2:己知正整數 n ,求 n!
Function fac(n)
If n <= 1 Then
Return n
Else
Return fac(n - 1) * n
End If
End Function
'Q3: 求兩個正整數 a, b 的最大公因數
Function GCD(a, b)
If b = 0 Then
Return a
Else
Return GCD(b, a Mod b)
End If
End Function
✩ 'Q3: lcm
Function lcm(ByVal a,ByVal b)
If b = 0 Then
Return a
Else
Return a * b / GCD(b, a Mod b)
End If
End Function
'Q4: 月兔問題 與 費氏數列(Fib)
Function Fib(n)
If n <= 1 Then
Return n
Else
Return Fib(n - 1) + Fib(n - 2)
End If
End Function
'Q5: 十進位數值轉二進位字串
Function Bin(n)
If n < 2 Then
Return n
Else
Return Bin(n \ 2) & Bin(n Mod 2)
End If
End Function
✩ 'Q5: Quad(n)
Function Quad(ByVal n)
If n < 4 Then
Return n
Else
Return Quad(n \ 4) & Quad(n Mod 4)
End If
End Function
End Module
'Q1:己知正整數 n ,求 1+2+3+...+ n 之和
'recursive 遞迴函數
Function sum(n)
If n <= 1 Then
Return n
Else
Return sum(n - 1) + n
End If
End Function
'Q1:sumOdd(n)
Function sumOdd(n)
If n <= 1 Then
Return n
Else
Return sumOdd(n - 2) + n
End If
End Function
'Q1:sumEven(n)
Function sumEven(n)
If n <= 1 Then
Return n
Else
Return sumEven(n - 2) + n
End If
End Function
'Q2:己知正整數 n ,求 n!
Function fac(n)
If n <= 1 Then
Return n
Else
Return fac(n - 1) * n
End If
End Function
'Q3: 求兩個正整數 a, b 的最大公因數
Function GCD(a, b)
If b = 0 Then
Return a
Else
Return GCD(b, a Mod b)
End If
End Function
✩ 'Q3: lcm
Function lcm(ByVal a,ByVal b)
If b = 0 Then
Return a
Else
Return a * b / GCD(b, a Mod b)
End If
End Function
'Q4: 月兔問題 與 費氏數列(Fib)
Function Fib(n)
If n <= 1 Then
Return n
Else
Return Fib(n - 1) + Fib(n - 2)
End If
End Function
'Q5: 十進位數值轉二進位字串
Function Bin(n)
If n < 2 Then
Return n
Else
Return Bin(n \ 2) & Bin(n Mod 2)
End If
End Function
✩ 'Q5: Quad(n)
Function Quad(ByVal n)
If n < 4 Then
Return n
Else
Return Quad(n \ 4) & Quad(n Mod 4)
End If
End Function
End Module
沒有留言:
張貼留言