Forum studentów Informatyki Zaocznej na Uniwersytecie Opolskim
metoda iteracyjna
Public Class Form1 Function MJciagLucasa(ByVal MJa As String) As Double Dim MJciag As Double Dim MJ1 As Integer = 2 Dim MJ2 As Integer = 1 If CInt(MJa) = 1 Then MJciag = 2 ElseIf CInt(MJa) = 2 Then MJciag = 1 ElseIf CInt(MJa) > 2 Then For i As Integer = 0 To CInt(MJa - 3) MJciag = MJ1 + MJ2 MJ1 = MJ2 MJ2 = MJciag Next End If Return MJciag End Function Private Sub MJ_bLiczCiag_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MJ_bLiczCiag.Click Dim wynik As Double wynik = MJciagLucasa(MJ_tbCiag.Text) MJ_lWynikCiagu.Text = CStr(wynik) End Sub End Class
metoda rekurencyjna
Public Class Form1 Function MJ_CiagLucasa(ByVal MJ_a As Double) As Double If MJ_a = 1 Then MJ_CiagLucasa = 2 MJ_a = 2 ElseIf MJ_a = 2 Then MJ_CiagLucasa = 1 MJ_a = 1 ElseIf MJ_a > 2 Then MJ_a = MJ_CiagLucasa(MJ_a - 1) + MJ_CiagLucasa(MJ_a - 2) End If Return MJ_a End Function Private Sub MJ_Button_licz_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MJ_Button_licz.Click MJ_Label_wynik.Text = CStr(MJ_CiagLucasa(CDbl(MJ_Textbox_NciaguL.Text))) End Sub End Class
po****ło go z tymi inicjałami
edit:
własnie widze ze w metodzie iteracyjnej mozna zmiejszyc ilosc zmiennych
Ostatnio edytowany przez ciemak666 (2008-12-23 23:31:41)
Offline
Użytkownik
Moja wersja:
Public Class Form1 Function DZlucrek(ByVal n As Integer) As Integer Dim wynik As Integer If n > 1 Then wynik = DZlucrek(n - 1) + DZlucrek(n - 2) ElseIf n = 1 Then wynik = 1 ElseIf n = 0 Then wynik = 2 End If Return wynik End Function Function DZluciter(ByVal n As Integer) As Integer If n = 0 Then Return 2 ElseIf n = 1 Then Return 1 ElseIf n > 1 Then Dim w1 As Integer = 2 Dim w2 As Integer = 1 Dim zam As Integer For i As Integer = 2 To n - 1 zam = w2 w2 = w1 + w2 w1 = zam Next w2 = w1 + w2 Return w2 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 = CInt(TextBox1.Text) Label1.Text = CStr(n) + " wyraz ciagu Lucasa to: " + CStr(DZlucrek(n)) End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim n As Integer = CInt(TextBox1.Text) Label1.Text = CStr(n) + " wyraz ciagu Lucasa to: " + CStr(DZluciter(n)) End Sub End Class
Było mówione coś czy te programy mają być idiotoodporne?
Offline