码迷,mamicode.com
首页 > 其他好文 > 详细

hdu1715

时间:2014-08-09 18:40:58      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   java   os   io   for   

bubuko.com,布布扣
import java.util.*;
import java.math.*;
import java.io.*;
class Main
{
    public static void main(String[] args)
    {
        BigInteger[] ans = new BigInteger[1000];
        ans[1]=ans[0]=BigInteger.ONE;
        for(int i=2;i<1000;i++)
            ans[i]=ans[i-1].add(ans[i-2]);   
        Scanner sc = new Scanner(new BufferedInputStream(System.in));
        int t = sc.nextInt();
        for(int i=0;i<t;i++) System.out.println(ans[sc.nextInt()-1]);
    }
}
数组记忆化存储
bubuko.com,布布扣
import java.util.*;
import java.math.*;
class Main
{
    public static void main(String[] args)
    {
        BigInteger ans,t_1,t_2;
        int n,t;        
        Scanner sc = new Scanner(System.in);
        t = sc.nextInt();
        for(int i=0;i<t;i++)
        {
            ans = BigInteger.valueOf(1);
            n = sc.nextInt();
            if(n>2)
            {
                t_1 = t_2 = ans;
                for(int j=3;j<=n;j++)
                {
                    ans=t_2.add(t_1);
                    t_2=t_1;                    
                    t_1=ans;
                }
            }
            System.out.println(ans);
        }
    }
}
循环变量相加求解

两种方法时间相差不大.

hdu1715,布布扣,bubuko.com

hdu1715

标签:style   blog   http   color   java   os   io   for   

原文地址:http://www.cnblogs.com/syzhd/p/3901324.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!