标签:
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 16771 Accepted Submission(s): 5537
1 #include <cstdio> 2 #include <cstring> 3 4 int fib[1005][215]; 5 6 void init() 7 { 8 memset(fib,0,sizeof(fib)); 9 fib[1][1] = 1; 10 fib[2][1] = 1; 11 int tmp; 12 int carry = 0; //进位 13 for(int i = 3; i <= 1000; ++i){ 14 for(int j = 1; j <= 209; ++j){ 15 tmp = fib[i-1][j]+fib[i-2][j]+carry; 16 fib[i][j] = tmp%10; 17 carry = tmp/10; 18 } 19 } 20 } 21 22 int main() 23 { 24 init(); 25 int t,n; 26 scanf("%d",&t); 27 while(t--){ 28 scanf("%d",&n); 29 int i; 30 for(i = 215; i>0 && fib[n][i] == 0; --i) 31 ; 32 for(; i>0; --i){ 33 printf("%d",fib[n][i]); 34 } 35 printf("\n"); 36 } 37 return 0; 38 }
1 import java.util.Scanner; 2 import java.math.BigInteger; 3 4 class Main{ 5 public static void main(String[] args) { 6 BigInteger[] bi = new BigInteger[1001]; 7 bi[1] = new BigInteger("1"); 8 bi[2] = new BigInteger("1");; 9 for(int i= 3; i <= 1000; ++i){ 10 bi[i] = bi[i-1].add(bi[i-2]); 11 } 12 Scanner in = new Scanner(System.in); 13 int t = in.nextInt(); 14 int n; 15 for(int i = 0; i<t; ++i){ 16 n = in.nextInt(); 17 System.out.println(bi[n]); 18 } 19 } 20 }
标签:
原文地址:http://www.cnblogs.com/inmoonlight/p/5439390.html