先java代码:
import java.util.Scanner; import java.math.*; public class Main { public static void main(String[] args) { Scanner cin = new Scanner(System.in); BigInteger fb[] = new BigInteger [1005]; fb[1] = BigInteger.valueOf(1); fb[2] = BigInteger.valueOf(1); for (int i = 3; i < 1005; i++) fb[i] = fb[i-1].add(fb[i-2]); int t = cin.nextInt(); while (t != 0) { t--; int n = cin.nextInt(); System.out.println(fb[n]); } cin.close(); } }
然后是C++代码:
#include<stdio.h> int fb[1001][100]; void add(int *s1,int *s2,int *s3) { int t=0; for(int i=0;i<100;i++) { s3[i]=(s1[i]+s2[i])%10000+t; t=(s1[i]+s2[i])/10000; } } void print(int *s) { for(int i=99;i>=0;i--) if(s[i]!=0) break; printf("%d",s[i--]); for(;i>=0;i--) printf("%04d",s[i]); puts(""); } int main() { int t,n; scanf("%d",&t); fb[1][0]=1; fb[2][0]=1; for(int i=3;i<=1000;i++) add(fb[i-1],fb[i-2],fb[i]); while(t--) { scanf("%d",&n); print(fb[n]); } return 0; }