zoukankan      html  css  js  c++  java
  • 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;
    
    }
  • 相关阅读:
    (转)深入理解JavaScript 模块模式
    (转)Javascript匿名函数的写法、传参、递归
    (转)javascript匿名函数的写法、传参和递归
    (转)初探Backbone
    (转)android平台phonegap框架实现原理
    (转)PhoneGap工作原理及需改进的地方
    (转)JQM 日期插件 mobiscroll Demo
    (转)jQuery Mobile 移动开发中的日期插件Mobiscroll 2.3 使用说明
    [题解] [笔记]期望&洛谷P3232
    [笔记] [题解] 状压$DP$&洛谷P1433
  • 原文地址:https://www.cnblogs.com/malloc/p/2491201.html
Copyright © 2011-2022 走看看