UVa 495 Fibonacci Freeze

求5000以内Fibonacci 数

代码
   
     
1 // 84ms 2010-05-18 9:51:27
2   // Type: Big integer(Fibonacci)
3 // 打表,对于单个数,fib[i][0]存位数,fib[i][1]~fib[i][n] 由低位到高位存,八位一存
4 #include < stdio.h >
5 #include < string .h >
6 #define EX 100000000
7 #define NL 1000
8
9 int fib[NL * 5 + 1 ][ 140 ];
10
11 inline void add( int K)
12 {
13 int I;
14 int x, y;
15 int a = K - 1 , b = K - 2 ;
16 y = 0 ;
17 for (I = 1 ; I <= fib[a][ 0 ]; I ++ ) {
18 x = fib[a][I] + fib[b][I] + y;
19 fib[K][I] = x % EX;
20 y = x / EX;
21 }
22 fib[K][ 0 ] = fib[a][ 0 ];
23 if (y > 0 ) {
24 fib[K][I] = y;
25 fib[K][ 0 ] ++ ;
26 }
27 }
28
29 int main()
30 {
31 int K, M, I;
32 memset(fib, 0 , sizeof (fib));
33 fib[ 0 ][ 0 ] = 1 ;
34 fib[ 0 ][ 1 ] = 0 ;
35 fib[ 1 ][ 0 ] = fib[ 1 ][ 1 ] = 1 ;
36 for (K = 2 ; K <= 5000 ; K ++ ) {
37 add(K);
38 }
39 while (scanf( " %d " , & M) != EOF) {
40 I = fib[M][ 0 ];
41 printf( " The Fibonacci number for %d is " , M);
42 printf( " %d " , fib[M][I]);
43 for (K = I - 1 ; K >= 1 ; K -- ) {
44 printf( " %08d " , fib[M][K]);
45 }
46 puts( "" );
47 }
48 return 0 ;
49 }

 

你可能感兴趣的:(fibonacci)