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 }

 

转载于:https://www.cnblogs.com/hduacm/archive/2012/08/22/2650623.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值