Uva11000 Bee

题目:

In Africa there is a very special species of bee. Every year, the female bees of such species give birth to one male bee, while the male bees give birth to one male bee and one female bee, and then they die!
Now scientists have accidentally found one “magical female bee” of such special species to the effect that she is immortal, but still able to give birth once a year as all the other female bees. The scientists would like to know how many bees there will be after N years. Please write a program that helps them find the number of male bees and the total number of all bees after N years.
Input
Each line of input contains an integer N (≥ 0). Input ends with a case where N = −1. (This case should NOT be processed.)
Output
Each line of output should have two numbers, the first one being the number of male bees after N years, and the second one being the total number of bees after N years. (The two numbers will notexceed 2的32次方.)
Sample Input
1
3
-1
Sample Output
1 2
4 7

题意:
有一种蜜蜂,每年一只雄蜂可以生育一只雄蜂和一只雌蜂,一只雌蜂可以生育一只雄蜂,之后它们便会死去。
现在有一只特殊的雌蜂,她不死并且可以每年生育一只雄蜂,问以这只雌蜂为起点,N年后有多少只雄蜂以及N年后总共有多少只蜜蜂。

递推:第二年的雌蜂=第一年的雄蜂+1(那只不死的雌蜂); 第二年的雄蜂=第一年总的蜜蜂.
注意:此题部分数据会超出int的表示范围。

参考代码:

#include 
#include 
#include 
using namespace std;
typedef long long ll;

class Bees {
    private:
        ll male;//雄蜂;
        ll female;//雌蜂;
        ll total;//总的蜜蜂;
    public:
        Bees() = default;
        Bees(ll male1, ll female1) : male(male1), female(female1) {}
        void caltotal() {total = male + female;}//计算总的蜜蜂数;
        ll getMale() const {return male;}//接口;
        ll getFemale() const {return female;}
        ll getTotal() const {return total;}
        void moni() {//递推;
            ll fe = female, ma = male;
            male += fe;
            female = ma + 1;
        }
};

typedef pair p;//first代表有多少只雄蜂, second代表有多少只蜜蜂;
vector

v; void init() { v.clear(); ll male = 0, female = 1, total = 0;//从那只不死的雌蜂开始; Bees *b = new Bees(male, female); b -> caltotal(); v.push_back(p(b -> getMale(), b -> getTotal())); for (int i = 1;i <= 1000000;++i) { b -> moni(); b -> caltotal(); v.push_back(p(b -> getMale(), b -> getTotal())); } delete b; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); int n; init(); while (cin >> n) { if (n == -1) break; if (n == 0) cout << 0 << " " << 1 << endl; else { cout << v[n].first << " " << v[n].second << endl; } } return 0; }

你可能感兴趣的:(Uva11000 Bee)