首页 > 代码库 > hdu1715

hdu1715

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]);    }}
数组记忆化存储
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);        }    }}
循环变量相加求解

两种方法时间相差不大.