运行 ❯
获取您
自己的
网站
×
更改方向
更改主题,深色/浅色
前往 Spaces
Python
C
Java
def nth_fibo(n): if n==0: return 0 if n==1: return 1 F = [None] * (n + 1) F[0] = 0 F[1] = 1 for i in range(2, n + 1): F[i] = F[i - 1] + F[i - 2] return F[n] n = 6 result = nth_fibo(n) print(f"The {n}th Fibonacci number is {result}") #Python
#include <stdio.h> int nthFibo(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]; } return F[n]; } int main() { int n = 6; int result = nthFibo(n); printf("The %dth Fibonacci number is %d\n", n, result); return 0; } //C
public class Main { public static int nthFibo(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]; } return F[n]; } public static void main(String[] args) { int n = 6; int result = nthFibo(n); System.out.println("The " + n + "th Fibonacci number is " + result); } } //Java
Python 结果
C 结果
Java 结果
第 6 个斐波那契数是 8
第 6 个斐波那契数是 8
第 6 个斐波那契数是 8