2012年2月15日 星期三

費氏數

由"in.txt"讀取一數字n求出第n個費氏數,費氏數定義如下:
0,當n=0時
f(n)= 1,當n=1時
f(n-1)+f(n-2),當n>=2時
在此列舉一串數列:0、1、1、2、3、5、8、13、21、34、55、89、144、233,這些就是由0<=n<=13所組成的費氏數。
※(0<=n<=1000)

輸入範例:8

輸出範例:21

10 則留言:

  1. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As 1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, n
    a = 0: b = 1
    For i = 1 To n - 1
    ans = a + b
    a = b
    b = ans
    Next
    Print #2, ans
    Close
    Close
    End
    End Sub

    回覆刪除
  2. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Dim x(13) As Integer
    Input #1, n
    x(0) = 0
    x(1) = 1
    For i = 2 To n
    x(i) = x(i - 1) + x(i - 2)
    Next i
    Print #2, x(n)
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  3. Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Dim x(100) As Integer
    Input #1, n
    x(0) = 0
    x(1) = 1
    For i = 2 To n
    x(i) = x(i - 1) + x(i - 2)
    Next i
    ans = x(n)
    Print #2, ans
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  4. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, x
    Print #2, f(x)
    Close
    Close
    End
    End Sub
    Function f(a)
    If a <= 2 Then
    ans = 1
    Else
    ans = f(a - 1) + f(a - 2)
    End If
    f = ans
    End Function

    回覆刪除
  5. Function f(x)
    If x <= 2 Then
    f = 1
    Else
    f = f(x - 1) + f(x - 2)
    End If
    End Function
    Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As 1
    Open App.Path & "\out.txt" For Output As 2
    Input #1, n
    Print #2, f(n)
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  6. Function f(n)
    If n = 0 Then f = 0
    If n = 1 Or n = 2 Then f = 1
    If n > 2 Then
    f = f(n - 1) + f(n - 2)
    End If
    End Function

    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, n
    Print #2, f(n)
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  7. package work;
    import java.io.*;
    public class Work{



    public static void main(String[] args) {
    String file = "c:\\in.txt";
    String Fo = "c:\\out.txt";
    String in="0";

    try{
    DataInputStream isr= new DataInputStream(new FileInputStream(file));
    BufferedReader b = new BufferedReader(new InputStreamReader(isr));
    FileOutputStream A = new FileOutputStream(Fo);
    OutputStreamWriter out = new OutputStreamWriter(A);
    //---
    String S = b.readLine();
    int s = Integer.parseInt(S);
    int A1=0,A2=1;
    int i;
    int ans = 0;
    for (i=1;i<=s-1;i++){ans=A1+A2;A1=A2;A2=ans;}
    out.write(String.valueOf(ans));
    //---
    out.close();
    }
    catch(Exception e){}
    }



    }

    回覆刪除
  8. 作者已經移除這則留言。

    回覆刪除
  9. Private Sub Form_Load()
    Dim arr() As Integer
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, n
    ReDim arr(Val(n))
    For i = 0 To n
    If i < 2 Then
    arr(0) = 0
    arr(1) = 1
    Else
    arr(i) = arr(i - 2) + arr(i - 1)
    End If
    Next i
    Print #2, arr(n)
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  10. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, n
    Print #2, f(n)
    Close #2
    Close #1
    End
    End Sub

    Function f(n)
    If n = 0 Then f = 0
    If n = 1 Or n = 2 Then f = 1
    If n > 2 Then f = f(n - 2) + f(n - 1)
    End Function

    回覆刪除