zoukankan      html  css  js  c++  java
  • 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;
        }
    }
    


  • 相关阅读:
    20189222 《网络攻防技术》第一周作业
    apue.h 运行UNIX环境高编程序
    fflush()函数
    线性链表如何选择二级指针还是一级指针
    scanf()gets()fgets()区别
    淺談Coach思考模式
    Hello World
    C语言I博客作业04
    python模块:logging
    python模块:subprocess
  • 原文地址:https://www.cnblogs.com/gray1566/p/3704284.html
Copyright © 2011-2022 走看看