Fibonacci 取余,直接做超时

 1 超时:
 2 import java.lang.*;
 3 import java.util.*;
 4 import java.text.*;
 5 import java.math.*;
 6 import java.io.*;
 7 class Main
 8 {
 9     public static void main(String []args)
10     {
11         Scanner cin=new Scanner(System.in);
12         BigInteger f[]=new BigInteger[1000];
13         BigInteger g=BigInteger.valueOf(10),e;
14         BigInteger l=BigInteger.valueOf(10000);
15         int i,n,m;
16         while(cin.hasNext())
17         {
18                 n=cin.nextInt();
19                 f[0]=BigInteger.valueOf(0);
20                 f[1]=BigInteger.valueOf(1);
21                 f[2]=BigInteger.valueOf(1);
22                 if(n>1)
23                 {
24                     for(i=2;i<=n;i++)
25                     {
26                         f[2]=f[1].add(f[0]);
27                         f[0]=f[1];
28                         f[1]=f[2];
29                     }
30                     e=f[2];
31                     while(e.compareTo(l)<0)
32                     {
33                         e=e.divide(g);
34                     }
35                     System.out.println(f[2]);
36                 }
37                 else if(n==1)System.out.println(1);
38                 else System.out.println(0);
39         }
40     }
41 }

 

 1 import java.math.*;
 2 import java.lang.*;
 3 import java.text.*;
 4 import java.util.*;
 5 import java.io.*;  
 6 class Main
 7 {
 8     public static void main(String[] args)
 9     {
10         Scanner cin=new Scanner(System.in);
11         int arr[]={0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765},n;   
12         while(cin.hasNext())
13         {
14             n=cin.nextInt();
15             if(n<=20)
16             {
17                 System.out.println(arr[n]);
18                 continue;
19             }
20             else
21             {
22                 double m=Math.log10(1.0/Math.sqrt(5))+n*Math.log10((1+Math.sqrt(5))/2);
23                 double p=m-(int)m;
24                 p=Math.pow(10,p);
25                 while(p<1000)
26                 {
27                     p*=10;
28                 }
29                 System.out.println((int)p);
30             }
31         }
32     }
33 }

 

posted @ 2012-08-22 12:48  疼痛落在指尖  阅读(192)  评论(0编辑  收藏  举报