百度之星题目

高精度加法,斐波那契数列

度熊面前有一个全是由1构成的字符串,被称为全1序列。你可以合并任意相邻的两个1,从而形成一个新的序列。对于给定的一个全1序列,请计算根据以上方法,可以构成多少种不同的序列。
Input
这里包括多组测试数据,每组测试数据包含一个正整数N,代表全1序列的长度。
1≤N≤200
Output
对于每组测试数据,输出一个整数,代表由题目中所给定的全1序列所能形成的新序列的数量。
Sample Input
1
3
5
Sample Output
1
3
8
对于m个数字组成串,我们进行一下划分,第一是以1开始的串,第二是以2开始的串,假设1开头
方法数 m1 = f(n-1) ,假设2开头,m2 = f(n-2) ,那么f(n) = f(n-1) + f(n-2),这样就证明了这是一个斐波那契数列。N因为实在太大,于是我们通过高精度加法完成,高精度实现使用我写的高精度模板

#include 
#include 
#include 
#include 
#include 
using namespace std;

class BigNum
{
private:
    //指向大数数据的指针,数据是反向存储的,从低位到高位
    int *value;
    //表示大数绝对值的位数
    int length;
    //表示大数的符号
    bool Positive;
    //高精度非负数加法实现,加法的两个大数要保证不为空,这个函数的实现也许和平台有关,我在杭电上提交以及本地测试都没问题,提交到南阳oj却返回了错误
    BigNum add(BigNum &t)
    {
        int *a=value;
        int *b=t.getValue();
        int len1=length;
        int len2=t.getLength();
        int len=(len1>len2?len1:len2)+1; //结果字符串长度初始化为长串长度+1,这是为了避免进位溢出
        int *ans=new int[len];
        memset(ans,0,len*sizeof(int)); //初始化为0
        int index=0;
        for(;index//a,b从低位开始相加,知道一方或两方无数可加
        {
            ans[index]+=a[index]+b[index];
            if(ans[index]>=10)
            {
                ans[index]-=10; //进位产生的变化
                ans[index+1]++;
            }
        }
        if(len1//a结束了,b还没结束
        {
            for(;indexif(len1>len2) //b结束了
        {
            for(;indexif(ans[len-1]==0) len--; //最高位相加是否进位
        char *str=new char[len+1]; //保证字符串可以正常结束
        memset(str,0,(len+1)*sizeof(char));
        for(int i=0;i1]+'0';
        BigNum temp(str,len);
        delete str;
        delete ans;
        return temp;
    }
    //高精度减法实现,保证a>b && a,b 都是非负数
    BigNum sub(BigNum &t)
    {
        int *a=value;
        int *b=t.getValue();
        int len1=length;
        int len2=t.getLength();
        int len=len1;
        int *ans=new int[len];
        memset(ans,0,len*sizeof(int));
        int index=0;
        for(;indexif(ans[index]<0)
            {
                ans[index]+=10;
                ans[index+1]--;
            }
        }
        for(;indexif(ans[index]<0)
            {
                ans[index]+=10;
                ans[index+1]--;
            }
        }
        for(int i=len-1;i>=0;i--)
        {
            if(!ans[i])
                len--;
            else
                break;
        }
        char *str=new char[len+1];
        memset(str,0,(len+1)*sizeof(char));
        for(int i=0;i1]+'0';
        }
        BigNum temp(str,len);
        delete str;
        delete ans;
        return temp;
    }
public:
    //默认构造函数
    BigNum(): value(NULL),length(0),Positive(true) {}
    //传参构造,从str串中拷贝len个字符,反向存储在value中
    BigNum(char *str,int len)
    {
        if(NULL==str || len<=0 || strlen(str)==0)
        {
            length=0;
            value=NULL;
            Positive=true;
            return;
        }
        if(str[0]=='-')
        {
            Positive=false;
            length=len-1;
            value= new int[length];//反向填充value数组
            for(int i=0;i'0';
            }
        }
        else
        {
            Positive=true;
            length=len;
            value= new int[length];//反向填充value数组
            for(int i=0; i1]-'0';
            }
        }
    }
    //复制构造函数,深度复制
    BigNum(const BigNum &t)
    {
        if(t.getLength()==0)
        {
            value=NULL;
            length=0;
            return;
        }
        Positive=t.isPositive();
        length=t.getLength();
        value = new int[length];
        memcpy(value,t.getValue(),length*sizeof(int));
    }
    ~BigNum() { delete []value; } //重要的析构函数
    int *getValue() const { return value; }
    int getLength() const { return length;}
    bool isPositive() const { return Positive; }
    //重载赋值运算符
    BigNum &operator=(const BigNum &t)
    {
        if(t.getLength()==0) // 判断空值的情况
        {
            length=0;
            delete[]value;
            value=NULL;
            return *this;
        }
        if(length>0) //复制前清空原来的内容
        {
            delete[] value;
            length=0;
        }
        Positive=t.isPositive();
        length=t.getLength();
        value = new int[length];
        memcpy(value,t.getValue(),length*sizeof(int));
        return *this; //实现连续赋值 例如 a=b=c;
    }
    // 重载输入运算符
    friend istream& operator>>(istream &ist,BigNum &t)
    {
        string str;
        ist>>str;
        if(t.length>0)
        {
            delete[] t.value;
            t.length=0;
        }
        t.Positive=true;
        if(str[0]=='-')
        {
            t.Positive=false;
            t.length=str.length()-1;
            t.value = new int[t.length];
            for(int i=0;i'0';
            }
            return ist;
        }
        else
        {
            t.Positive=true;
            t.length = str.length();
            t.value = new int[t.length];
            for(int i=0;i1]-'0';
            }
            return ist;
        }
    }
    //重载输出运算符
    friend ostream& operator<<(ostream &ostr,const BigNum &t)
    {
        //cout<
        if(t.length==0) // 判断t为空值的情况
        {
            return ostr;
        }
        if(!t.isPositive())
        {
            ostr<<'-';
        }
        for(int i=t.length-1;i>=0;i--) //反向输出才是从高位到低位输出s
        {
            ostr<return ostr;
    }
    BigNum abs()
    {
        BigNum ans=*this;
        ans.Positive=true;
        return ans;
    }
    bool operator==(const BigNum &t) //重载==运算符
    {
        if( Positive!=t.isPositive() || length!=t.getLength())
            return false;
        for(int i=0;iif(value[i]!=t.getValue()[i]) return false;
        }
        return true;
    }
    bool operator<(const BigNum &t) // 重载<运算符
    {
        if(!Positive && t.isPositive()) return true; // 前负后正
        if(Positive && !t.isPositive())  return false;// 前正后负
        if(Positive && t.isPositive())
        {
            if(lengthreturn true;
            else if(length>t.getLength())
                return false;
            for(int i=length-1; i>=0; i--)
            {
                if(value[i]return true;
                else if(value[i]>t.getValue()[i]) return false;
            }
            return false;
        }
        if(!Positive && !isPositive())
        {
            if(lengthreturn false;
            else if(length>t.getLength())
                return true;
            for(int i=length-1; i>=0; i--)
            {
                if(value[i]return false;
                else if(value[i]>t.getValue()[i]) return true;
            }
            return false;
        }
    }
    bool operator>(const BigNum &t) //重载>运算符
    {
        if(operator<(t) || operator==(t))
            return false;
        return true;
    }
    bool operator>=(const BigNum &t) //重载>=运算符
    {
        if(operator>(t) || operator==(t))
            return true;
        return false;
    }
    bool operator<=(const BigNum &t) //重载<=运算符
    {
        if(operator<(t) || operator==(t)) return true;
        return false;
    }
    BigNum operator+(BigNum &t)
    {
        if(Positive && t.isPositive())//正数加正数
        {
            BigNum ans=add(t);
            return ans;
        }
        if(Positive && !t.isPositive())//正数加负数
        {
            BigNum t1=abs();
            BigNum t2=t.abs();
            if(t1false;
                return ans;
            }
            else if(t1>t2)
            {
                BigNum ans=t1.sub(t2);
                ans.Positive=true;
                return ans;
            }
            else
            {
                BigNum ans("0",1);
                return ans;
            }
        }
        if(!Positive && t.isPositive())//负数加正数
        {
            BigNum t1=abs();
            BigNum t2=t.abs();
            if(t1true;
                return ans;
            }
            else if(t1>t2)
            {
                BigNum ans=t1.sub(t2);
                ans.Positive=false;
                return ans;
            }
            else
            {
                BigNum ans("0",1);
                return ans;
            }
        }
        if(!Positive && !t.isPositive())//负数加负数
        {
            BigNum t1=abs();
            BigNum t2=t.abs();
            BigNum ans=t1.add(t2);
            ans.Positive=false;
            return ans;
        }
    }
    BigNum operator-(BigNum &t)
    {
        if(Positive && t.isPositive())//正数减正数
        {
            if(operator<(t))
            {
                BigNum ans=t.sub(*this);
                ans.Positive=false;
                return ans;
            }
            else if(operator>(t))
            {
                BigNum ans=sub(t);
                ans.Positive=true;
                return ans;
            }
            else if(operator==(t))
            {
                BigNum ans("0",1);
                return ans;
            }
        }
        if(Positive && !t.isPositive())//正数减负数
        {
            BigNum t2=t.abs();
            BigNum ans=add(t2);
            ans.Positive=true;
            return ans;
        }
        if(!Positive && t.isPositive())//负数减正数
        {
            BigNum t1=abs();
            BigNum ans=t1.add(t);
            ans.Positive=false;
            return ans;
        }
        if(!Positive && !t.isPositive())//负数减负数
        {
            BigNum t1=abs();
            BigNum t2=t.abs();
            if(t1true;
                return ans;
            }
            else if(t1>t2)
            {
                BigNum ans=t1.sub(t2);
                ans.Positive=false;
                return ans;
            }
            else if(t1==t2)
            {
                BigNum ans("0",1);
                return ans;
            }
        }
    }
};

int main()
{
    BigNum f[205];
    BigNum x("1",1);
    BigNum y("2",1);
    f[1]=x; f[2]=y;
    for(int i=3;i<=200;i++)
    {
        BigNum z = x + y;
        f[i]=z;
        x=y; y=z;
    }
    int i;
    while(scanf("%d" ,&i)!=EOF)
    {
        cout << f[i] << endl;
    }
    return 0;
}

你可能感兴趣的:(曾经水过的题)