2017年12月13日 星期三

[ VB2010 ] -用遞迴求陣列元素總和

[ VB2010 ] -用遞迴求陣列元素總和



Public Class Form1

    Public DATA(100) As Integer
    Public num1, I As Integer

    Private Function rsum(ByVal DATA() As Integer, ByVal num1 As Integer) '{ /*遞迴地求算陣列元素總和*/
        If (num1 < 1) Then
            Return 0
        Else
            Return (rsum(DATA, num1 - 1) + DATA(num1))  '/*遞迴地求剩下解*/
        End If

    End Function


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox4.Text = ""
        num1 = Val(TextBox2.Text)
        TextBox4.Text &= CStr(rsum(DATA, (num1)) + DATA(0))

    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        End
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        I = 0

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim K As Integer
        num1 = Val(TextBox2.Text)
        If I <= (num1 - 1) Then
            DATA(I) = Val(TextBox3.Text)
            Label4.Text = "[" & (I).ToString & "]"
        Else
            TextBox3.Text = "已完成輸入"
            For K = 0 To num1 - 1
                TextBox5.Text &= DATA(K) & ","
            Next

        End If
        I += 1

    End Sub


    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim k As Integer
        TextBox4.Text = ""
        TextBox5.Text = ""
        TextBox3.Text = ""
        TextBox2.Text = ""
        I = 0
        For k = 0 To 99
            DATA(I) = 0
        Next

    End Sub
End Class

[ VB2010 ] -用遞迴方法 求輸入兩數字A, B,利用遞迴求得A的B次方

[ VB2010 ] -用遞迴方法  求輸入兩數字A, B,利用遞迴求得A的B次方




Public Class Form1


    Private Function power(ByVal a As Integer, ByVal b As Integer) '/*遞迴程式部分*/

        If (b = 0) Then
            Return 1
        ElseIf (b = 1) Then
            Return a
        Else
            Return (a * power(a, b - 1))
        End If

    End Function


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim a, b As Integer

        a = Val(TextBox2.Text)
        b = Val(TextBox3.Text)

        TextBox4.Text = power(a, b).ToString()

    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        End
    End Sub

End Class


[ VB2010 ] -用遞迴方法-輸入任意兩個數字,以遞迴方式求出最大公因數。


[ VB2010 ] --用遞迴方法  輸入任意兩個數字,以遞迴方式求出最大公因數。


Public Class Form1


    Private Function gcd(ByVal b As Integer, ByVal R As Integer) '/*遞迴程式部分*/
        Dim a As Integer

        If (b > 0) Then
            a = R
            R = b
            b = a Mod R
            Return gcd(b, R)
        Else

            Return R

        End If

    End Function


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim num1, num2, R, b As Integer

        num1 = Val(TextBox2.Text)
        num2 = Val(TextBox3.Text)


        b = num1 Mod num2
        R = num2
        R = gcd(b, R)

        TextBox4.Text = gcd(b, R).ToString()

    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        End
    End Sub

End Class



算數運算子:
運算子說明範例
+1 + 1 = 2
-2 - 1 = 1
*2 * 3 = 6
/13 / 8 = 1.625
\( 只取整數部分) 13 \ 8 = 1
Mod取餘數13 Mod 8 = 5 、26 Mod 13 = 0
^次方2 ^ 3 = 8
設定運算子:

運算子說明 (作用)範例(number = 10)
=等於 (設值)number = 10
+ =加之後設定number += 2(相當於number = number + 2)
- =減之後設定number -= 2(相當於number = number - 2)
* =乘之後設定number *= 2(相當於 number = number * 2)
/ =除之後設定number /= 2(相當於number = number / 2)
\ =除之後設定number \= 2(相當於 number = number \2)(只取整數部分)
^ =次方之後設定number ^ = 2(相當於 number = number ^ 2)
&=字串相加之後設定number &= 2(相當於number = number &2)

[ VB2010 ] -用遞迴 求算1*2+2*3+3*4+…+(n-1)*n之和.

[ VB2010 ] -用遞迴 求算1*2+2*3+3*4+…+(n-1)*n之和.




Public Class Form1


    Private Function sum(ByVal n As Integer) '/*遞迴程式部分*/
        If (n = 1) Then
            Return 0
        Else
            Return sum(n - 1) + n * (n - 1)
        End If


    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim n As Integer
        n = Val(TextBox2.Text)
        TextBox3.Text = sum(n).ToString()
    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        End
    End Sub
End Class

[ VB2010 ] --用遞迴方法 輸入一個整數n,並用遞迴求解1+2+3…n之和.

[ VB2010 ] --用遞迴方法 輸入一個整數n,並用遞迴求解1+2+3…n之和.  




Public Class Form1


    Private Function sum(ByVal n As Integer) '/*遞迴程式部分*/
        If n = 1 Then
            Return 1
        Else
            Return sum(n - 1) + n
        End If

    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim n As Integer
        n = Val(TextBox2.Text)
        TextBox3.Text = sum(n).ToString()
    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        End
    End Sub
End Class


[ VB2010 ] - 遞迴程式 --輸入兩個數M及N,計算M取N的值



[ VB2010 ] - 遞迴程式 --輸入兩個數M及N,計算M取N的值

輸入兩個數M及N,計算M取N的值,亦即M!/(N!(M-N)!)
/* 程式功能: 輸入兩個數M及N,計算M取N的值,亦即M!/(N!(M-N)!) */





Public Class Form1
    Private Function factorial(ByVal n As Integer) '/*遞迴程式部分*/
        If n = 1 Then
            Return 1
        Else
            Return n * factorial(n - 1)
        End If

    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim m, n As Integer
        m = Val(TextBox2.Text)
        n = Val(TextBox3.Text)
        TextBox4.Text = (factorial(m) / (factorial(n) * factorial(m - n))).ToString

    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        End
    End Sub
End Class