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;
    
    }
  • 相关阅读:
    Idea 找不到tomcat server 服务器选项
    idea永久使用
    Jax
    我的2014
    Mysql锁机制
    mysql 查询效率提升
    Java 静态工厂方法
    java 面试题(转)
    Java Semaphore
    Java CyclicBarrier
  • 原文地址:https://www.cnblogs.com/malloc/p/2491201.html
Copyright © 2011-2022 走看看