zoukankan      html  css  js  c++  java
  • UVA 11000- 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 not
    exceed 232.)
    Sample Input
    1
    3
    -1
    Sample Output
    1 2
    4 7

    题意: 蜜蜂,每年每只雄蜂产下一只雌蜂和一只雄蜂,每只雌蜂产下一只雄蜂,然后就死去, 现在发现了一只不会死的雌蜂,问以她为起始点,第N年有多少雄蜂和一共多少蜜蜂。

    题解:    设第i年的雄蜂和雌蜂分别为a(i)与s(i),则有如下递推关系:

                1 s(i)= a(i-1)+ 1

                2 a(i)= s(i-1)+ a(i-1)

                整理得:a(i)=a(i-1)+ a(i-2)+ 1;

    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    #include<vector>
    #include <map>
    using namespace std ;
    typedef long long ll;
    
    const int N=100+10;
    const int maxn = 1e9 + 1;
    
    ll a[N];
    void init() {
        a[1] = 1;
        a[0] = 0;
        for(int i=2;i<=N;i++) a[i] = a[i-1] + a[i-2] +1ll;
    }
    int main() {
        init();
        int n;
        while(~scanf("%d",&n)) {
            if(n == -1) break;
            printf("%lld %lld
    ", a[n] , a[n+1]);
        }
        return 0;
    }
    代码
  • 相关阅读:
    檢查 cpu 的全部 gpio 狀態及設定
    device tree 負值 property 寫法
    shell script timer and 無限迴圈
    vim 搜尋取代功能
    英国人是这样“造”单词的
    计划时间程序
    程序猿的崛起,一篇文章看懂编程语言
    如何读懂计算机(二进制)
    为什么计算机上能看到动人的图片,精彩的视频和悦耳的音乐?(二进制)
    格式化时间用了YYYY-MM-dd,元旦当天老板喊我回去改Bug!(转载)
  • 原文地址:https://www.cnblogs.com/zxhl/p/5124314.html
Copyright © 2011-2022 走看看