1 solutions

  • 0
    @ 2024-12-10 23:55:33

    C :

    #include<stdio.h>
    
    int step(int n) {
        if (n == 1) {
            return 1;
        }
        if (n == 2) {
            return 2;
        }
        return step(n - 1) + step(n - 2);
    }
    
    int main() {
        int n;
        while(~scanf("%d", &n)) {
            printf("%d\n", step(n));
        }
        return 0;
    }
    

    C++ :

    #include <iostream>
    using namespace std;
    int n,a,b,c;
    long long d;
    int main()
    {
       cin>>n;
       if (n<=2) cout<<n;
       else
        {a=1;b=2;
        for (d=2;d<=n-1;d++)
        {c=a+b;
         a=b;
         b=c;}
         cout<<c;}
        //system("pause");
        return 0;
    }
    
    

    Pascal :

    var i,j,n:integer;
        a:array[0..1000]of longint;
    begin
      readln(n);
      a[1]:=1;
      a[2]:=2;
      for i:=3 to n do a[i]:=a[i-1]+a[i-2];
      write(a[n]);
    end.
    
    

    Python :

    # coding=utf-8
    n=int(input())
    if n==1:
        s=1
    if n==2:
        s=2
    if n>=3:
        a=1
        b=2
        
        for i in range(3,n+1):
            c=a+b
            a=b
            b=c
        s=c
    print(s)
        
    
    • 1

    Information

    ID
    642
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By