原文:

给定一个数字 n,然后以相反的顺序打印 n 个斐波那契数列的项。

示例:

input : n = 5
output : 3 2 1 1 0
input : n = 8
output : 13 8 5 3 2 1 1 0

算法

1) 声明一个大小为 n 的数组。 2) 分别初始化a【0】a【1】013) 运行从 2n-1 的循环,并将a【i-2】a【i-1】的 之和存储在a【i】中。 4) 按相反顺序打印数组。

c

// cpp program to print fibonacci
// series in reverse order
#include 
using namespace std;
void reversefibonacci(int n)
{
    int a[n];
    // assigning first and second elements
    a[0] = 0;
    a[1] = 1;
    for (int i = 2; i < n; i  ) {
        // storing sum in the
        // preceding location
        a[i] = a[i - 2]   a[i - 1];
    }
    for (int i = n - 1; i >= 0; i--) {
        // printing array in
        // reverse order
        cout << a[i] << " ";
    }
}
// driver function
int main()
{
    int n = 5;
    reversefibonacci(n);
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java program to print fibonacci
// series in reverse order
import java.io.*;
class gfg {
    static void reversefibonacci(int n)
    {
        int a[] = new int[n];
        // assigning first and second elements
        a[0] = 0;
        a[1] = 1;
        for (int i = 2; i < n; i  )
        {
            // storing sum in the
            // preceding location
            a[i] = a[i - 2]   a[i - 1];
        }
        for (int i = n - 1; i >= 0; i--)
        {
            // printing array in
            // reverse order
            system.out.print(a[i]  " ");
        }
    }
    // driver function
    public static void main(string[] args)
    {
        int n = 5;
        reversefibonacci(n);
    }
}
// this code is contributed by vt_m.

python 3

# python 3 program to print fibonacci
# series in reverse order
def reversefibonacci(n):
    a = [0] * n
    # assigning first and second elements
    a[0] = 0
    a[1] = 1
    for i in range(2, n): 
        # storing sum in the
        # preceding location
        a[i] = a[i - 2]   a[i - 1]
    for i in range(n - 1, -1 , -1): 
        # printing array in
        # reverse order
        print(a[i],end=" ")
# driver function
n = 5
reversefibonacci(n)

c

// c# program to print fibonacci
// series in reverse order
using system;
class gfg {
    static void reversefibonacci(int n)
    {
        int []a = new int[n];
        // assigning first and second elements
        a[0] = 0;
        a[1] = 1;
        for (int i = 2; i < n; i  )
        {
            // storing sum in the
            // preceding location
            a[i] = a[i - 2]   a[i - 1];
        }
        for (int i = n - 1; i >= 0; i--)
        {
            // printing array in
            // reverse order
            console.write(a[i]  " ");
        }
    }
    // driver function
    public static void main()
    {
        int n = 5;
        reversefibonacci(n);
    }
}
// this code is contributed by vt_m.

服务器端编程语言(professional hypertext preprocessor 的缩写)

= 0; $i--)
    {
        // printing array in
        // reverse order
        echo($a[$i] . " ");
    }
}
// driver code
$n = 5;
reversefibonacci($n);
// this code is contributed by ajit.
?>

java 描述语言


输出:

3 2 1 1 0

时间复杂度: o(n)

辅助空间: o(n)