蜜蜂问题- 斐波那契数列

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/PR_sc/article/details/77234942

这里写图片描述题目描述

    一只蜜蜂在上图所示的数字蜂房上爬动,已知它只能从标号小的蜂房爬到标号大的相邻蜂房,现在问你:蜜蜂从蜂房M开始爬到蜂房N,M<N,有多少种爬行路线? 

输入

输入M,N的值。
输出

爬行有多少种路线。
样例输入

1 14
样例输出

377

分析
1-1 1
1-2 1
1-3 2
1-4 3
1-5 5
1-6 8
…….
斐波那契数列。
大数相加。

#include<bits/stdc++.h>
using namespace std;
string f1,f2,f;
int n,m;
string P  (string & num,string add){
    int g=0;
    if(num.length()<add.length()){
        string t=num;
        num=add;
        add=t;
    }
    string t (num.length()-add.length(),'0');
    add= t+add;
    int len1=num.length(),len2=add.length();
    for(int i=len1-1;i>=0;i--){
        int t=((num[i]-'0') +(add[i]-'0') + g);
        num[i]=t%10+'0';
        g=t/10;
    }
    if(g!=0){
        num.insert(0,string(1,(char)g+'0'));
    }
    return num;
}
int main (void){
    while(cin>>m>>n&&n &&m){
        f1="1";
        f2="2";
        for(int i=3;i<=n-m;i++){
            f=P(f1,f2);
            f1=f2;
            f2=f;
        }
        cout<<f2<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/PR_sc/article/details/77234942
今日推荐