hdu 1715 大菲波数(高精度数)

 

Problem Description

Fibonacci数列,定义如下:
f(1)=f(2)=1
f(n)=f(n-1)+f(n-2) n>=3。
计算第n项Fibonacci数值。

 

 

Input

输入第一行为一个整数N,接下来N行为整数Pi(1<=Pi<=1000)。

 

 

Output

输出为N行,每行为对应的f(Pi)。

 

 

Sample Input

5

1

2

3

4

5

 

 

Sample Output

1

1

2

3

5

 /***************

继续大数。

用 hdu 1047 写的 高精度加法模板求出1000个斐波那契数,额,用字符串打表

****************/

Code:

#include<iostream>
#include<string>
using namespace std;
string add(string x,string y)
{
    string ans ;
    int lenx = x.length();
    int leny = y.length();
    if(lenx<leny)
    {
        for(int i = 1;i<=leny-lenx;i++)
            x = "0"+x;
    }
    else
    {
        for(int i = 1;i<=lenx-leny;i++)
            y = "0"+y;
    }
    lenx = x.length();
    int cf = 0;
    int temp;
    for(int i = lenx-1;i>=0;i--)
    {
        temp = x[i] - '0' + y[i] - '0'+cf;
        cf = temp/10;
        temp%=10;
        ans = char('0'+temp)+ans;
    }
    if(cf!=0)
        ans = char(cf+'0')+ans;
    return ans;
}

int main()
{
    //cout<<add("5","8");
    int t,n;
    string x,num[1005];;
    cin>>t;
    num[1] = num[2] = "1";
    for(int i = 3;i<=1000;i++)
        num[i] = add(num[i-1],num[i-2]);
    while(t--)
    {

        cin>>n;
        cout<<num[n]<<endl;
    }
}


原文地址:https://www.cnblogs.com/gray1566/p/3704284.html