- Forum studentów Informatyki Zaocznej na Uniwersytecie Opolskim http://www.informatykauo.pun.pl/index.php - Programowanie http://www.informatykauo.pun.pl/viewforum.php?id=7 - Ciąg Lucasa metoda iteracyjna i rekurencyjna http://www.informatykauo.pun.pl/viewtopic.php?id=56 |
ciemak666 - 2008-12-23 23:27:47 |
metoda iteracyjna Kod: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 Kod: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 |
wieslawski - 2009-01-01 20:32:56 |
Moja wersja: Kod: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? |
Andrzej - 2009-01-05 14:20:33 |
Te programy mają wypisywać tylko n-ty wyraz ciągu czy też n początkowych wyrazów oraz wyraz większy i mniejszy? |