运行 ❯
获取你
自己的
网站
×
更改方向
更改主题,深色/浅色
转到 Spaces
Python
C
Java
def fibonacci_tabulation(n): if n == 0: return 0 elif n == 1: return 1 F = [0] * (n + 1) F[0] = 0 F[1] = 1 for i in range(2, n + 1): F[i] = F[i - 1] + F[i - 2] print(F) return F[n] n = 10 result = fibonacci_tabulation(n) print(f"\nThe {n}th Fibonacci number is {result}") #Python
#include <stdio.h> int fibonacciTabulation(int n) { if (n == 0) return 0; if (n == 1) return 1; int F[n + 1]; F[0] = 0; F[1] = 1; for (int i = 2; i <= n; i++) { F[i] = F[i - 1] + F[i - 2]; } for (int i = 0; i <= n; i++) { printf("%d ", F[i]); } printf("\n"); return F[n]; } int main() { int n = 10; int result = fibonacciTabulation(n); printf("\nThe %dth Fibonacci number is %d\n", n, result); return 0; } //C
public class Main { public static int fibonacciTabulation(int n) { if (n == 0) return 0; if (n == 1) return 1; int[] F = new int[n + 1]; F[0] = 0; F[1] = 1; for (int i = 2; i <= n; i++) { F[i] = F[i - 1] + F[i - 2]; } for (int num : F) { System.out.print(num + " "); } System.out.println(); return F[n]; } public static void main(String[] args) { int n = 10; int result = fibonacciTabulation(n); System.out.println("\nThe " + n + "th Fibonacci number is " + result); } } //Java
Python 结果
C 结果
Java 结果
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
第 10 个斐波那契数是 55
0 1 1 2 3 5 8 13 21 34 55
第 10 个斐波那契数是 55
0 1 1 2 3 5 8 13 21 34 55
第 10 个斐波那契数是 55