• ZOJ Problem Set–1828 Fibonacci Numbers


    Time Limit: 2 Seconds      Memory Limit: 65536 KB


    A Fibonacci sequence is calculated by adding the previous two members of the sequence, with the first two members being both 1.

    f(1) = 1, f(2) = 1, f(n > 2) = f(n - 1) + f(n - 2)

    Your task is to take a number as input, and print that Fibonacci number.


    Sample Input

    100

    Sample Output

    354224848179261915075

    Note:

    No generated Fibonacci number in excess of 1000 digits will be in the test data, i.e. f(20) = 6765 has 4 digits.


    Source: University of Waterloo Local Contest 1996.10.05

    #include<iostream>
    
    #include<vector>
    
    #include<string>
    
    using namespace std;
    
    const short NUMLENGTH = 1001;
    
    class BigInteger
    
    {
    
    private:
    
      unsigned short *num;
    
      size_t size;
    
      BigInteger& addBigInteger(const BigInteger& pre1, const BigInteger& pre2) const
    
      {
    
        BigInteger *result = new BigInteger();
    
        size_t loop = pre1.size > pre2.size ? pre1.size:pre2.size;
    
        bool carry = false;//标记运算是否有进位
    
        short tempResult;
    
        size_t i = 0;
    
        for(i = 0; i < loop; i++)
    
        {
    
          tempResult = *(pre1.num + i)*(i < pre1.size) + *(pre2.num + i)*(i < pre2.size) + (short)carry;
    
          carry = (tempResult >= 10);
    
          *(result->num + i) = tempResult - 10*carry;
    
        }
    
        *(result->num + i) = (short)carry;
    
        result->size = loop + (int)carry;
    
        return *result;
    
      }
    
    public:
    
      BigInteger()
    
      {
    
        num = new unsigned short[NUMLENGTH];
    
        for(int i = 0; i < NUMLENGTH; i++)
    
          *(this->num + i) = 0;
    
        size = 0;
    
      }
    
      BigInteger(const BigInteger& bi)
    
      {
    
        num = new unsigned short[NUMLENGTH];
    
        this->size = bi.size;
    
        for(int i = 0; i < NUMLENGTH;i++)
    
          *(this->num + i) = *(bi.num + i);
    
      }
    
      BigInteger(const string& bi)
    
      {
    
        this->size = bi.length();
    
        num = new unsigned short[NUMLENGTH];
    
        for(int i = size - 1; i >= 0; i--)
    
        {
    
          *(this->num + i) = bi[i] - '0';
    
        }
    
        for(int j = size; j < NUMLENGTH; j++)
    
        {
    
          *(this->num + j) = 0;
    
        }
    
      }
    
      BigInteger& operator+(const BigInteger& op) const
    
      {
    
        return addBigInteger(*this, op);
    
      }
    
      
    
      void printInteger()
    
      {
    
        for(int i = size - 1; i >= 0; i--)
    
          cout<<*(num + i);
    
      }
    
      void printDataArr()
    
      {
    
        for(int i = 0; i < NUMLENGTH; i++)
    
          cout<<*(num + i);
    
      }
    
      ~BigInteger()
    
      {
    
        delete [] num;
    
      }
    
    };
    
    int main(void)
    
    {
    
      vector<BigInteger> fabonacci;
    
      fabonacci.push_back(BigInteger("0"));
    
      fabonacci.push_back(BigInteger("1"));
    
      fabonacci.push_back(BigInteger("1"));
    
      int num;
    
      while(cin>>num)
    
      {
    
        if(fabonacci.size() <= num)
    
        {
    
          for(int i = fabonacci.size() - 1;i < num; i++)
    
            fabonacci.push_back(fabonacci[i] + fabonacci[i - 1]);
    
        }
    
        fabonacci[num].printInteger();
    
        cout<<endl;
    
      }
    
      return 0;
    
    }
  • 相关阅读:
    TeeChart的X轴,使用伪装的时间
    线程池
    修练8年C++面向对象程序设计之体会
    使用RESTClient插件数据模拟(GET,POST)提交
    :施密特建议尾随年轻的专业人士了解技术公司
    社会保障系列1《介绍》
    Centos根据系统VPS安装SendMail组件使WordPress支持E-mail
    Codeforces Round #107 (Div. 2)---A. Soft Drinking
    [Unity3D]Unity3D游戏开发3D选择场景中的对象,并显示轮廓效果强化版
    怎么样ubuntu 64 11.04 在执行32位程序
  • 原文地址:https://www.cnblogs.com/malloc/p/2491201.html
Copyright © 2020-2023  润新知