递推与递归

A - 养兔子

Description

一对成熟的兔子每月能且只能产下一对小兔子,每次都生一公一母,每只小兔子的成熟期是一个月,而成熟后的第二个月才开始生小兔。某人领养了一对小兔子,一公一母,请问第N个月以后,他将会得到多少对兔子。

Input

测试数据包括多组,每组一行,为整数n(1≤n≤90)。 输入以0结束。

Output

对应输出第n个月有几对兔子(假设没有兔子死亡现象,而且是一夫一妻制)。

Sample Input

1
2
0

Sample Output

1
2

Hint

数据类型可以用64位整数:long long

 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
using namespace std;
typedef long long ll;
ll a[91]={0,1,2,0};
ll yang(ll n)
{
    if(a[n]>0)
        return a[n];
    a[n]=yang(n-1)+yang(n-2);
    return a[n];
}
int main()
{
    ll n;
    while(cin>>n&&n)
    {
        ll ans=yang(n);
        cout<<ans<<endl;
    }
    return 0;
}

 C - 蟠桃记

Description

孙悟空在大闹蟠桃园的时候,第一天吃掉了所有桃子总数一半多一个,第二天又将剩下的桃子吃掉一半多一个,以后每天吃掉前一天剩下的一半多一个,到第n天准备吃的时候只剩下一个桃子。这下可把神仙们心疼坏了,请帮忙计算一下,第一天开始吃的时候桃子一共有多少个桃子。

Input

输入数据有多组,每组占一行,包含一个正整数n(1≤n≤30),表示只剩下一个桃子的时候是在第n天发生的。 输入以0结束。

Output

对于每组输入数据,输出第一天开始吃的时候桃子的总数,每个测试实例占一行。

Sample Input

2
4
0

Sample Output

4
22
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
using namespace std;
typedef long long ll;
ll yang(ll n)
{
    if(n==1)
        return 1;
    return 2*(yang(n-1)+1);
}
int main()
{
    ll n;
    while(cin>>n)
    {
        if(n==0)
            break;
        ll ans=yang(n);
        cout<<ans<<endl;
    }
    return 0;
}

 D - 算法:骨牌铺方格

Description

在2×n的一个长方形方格中,用一个1× 2的骨牌铺满方格,输入n ,输出铺放方案的总数. 例如n=3时,为2× 3方格,骨牌的铺放方案有三种,如下图:

 

Input

输入数据由多行组成,每行包含一个整数n,表示该测试实例的长方形方格的规格是2×n (0< n<=50)。

Output

对于每个测试实例,请输出铺放方案的总数,每个实例的输出占一行。

Sample Input

1
3
2

Sample Output

1
3
2
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
using namespace std;
typedef long long ll;
ll a[91]={0,1,2,3,0};
ll yang(ll n)
{
    if(a[n]>0)
        return a[n];
    a[n]=yang(n-1)+yang(n-2);
    return a[n];
}
int main()
{
    ll n;
    while(cin>>n)
    {
        ll ans=yang(n);
        cout<<ans<<endl;
    }
    return 0;
}

 

E - 不容易系列之一

Description

大家常常感慨,要做好一件事情真的不容易,确实,失败比成功容易多了!
做好“一件”事情尚且不易,若想永远成功而总从不失败,那更是难上加难了,就像花钱总是比挣钱容易的道理一样。
话虽这样说,我还是要告诉大家,要想失败到一定程度也是不容易的。比如,我高中的时候,就有一个神奇的女生,在英语考试的时候,竟然把40个单 项选择题全部做错了!大家都学过概率论,应该知道出现这种情况的概率,所以至今我都觉得这是一件神奇的事情。如果套用一句经典的评语,我们可以这样总结: 一个人做错一道选择题并不难,难的是全部做错,一个不对。

不幸的是,这种小概率事件又发生了,而且就在我们身边:
事情是这样的——HDU有个网名叫做8006的男性同学,结交网友无数,最近该同学玩起了浪漫,同时给n个网友每人写了一封信,这都没什么,要命的是,他竟然把所有的信都装错了信封!注意了,是全部装错哟!

现在的问题是:请大家帮可怜的8006同学计算一下,一共有多少种可能的错误方式呢?

Input

输入数据包含多个多个测试实例,每个测试实例占用一行,每行包含一个正整数n(1<n<=20),n表示8006的网友的人数。

Output

对于每行输入请输出可能的错误方式的数量,每个实例的输出占用一行。

Sample Input

2
3

Sample Output

1
2
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
using namespace std;
typedef long long ll;
ll a[91]={0,1,2,3,0};
ll yang(ll n)
{
    if(n==1)
        return 0;
    if(n==2)
        return 1;
    if(n==3)
        return 2;
    return (n-1)*(yang(n-2)+yang(n-1));
}
int main()
{
    ll n;
    while(cin>>n)
    {
        ll ans=yang(n);
        cout<<ans<<endl;
    }
    return 0;
}

F - 超级楼梯

Description

有一楼梯共M级,刚开始时你在第一级,若每次只能跨上一级或二级,要走上第M级,共有多少种走法?

Input

输入数据首先包含一个整数N,表示测试实例的个数,然后是N行数据,每行包含一个整数M(1<=M<=40),表示楼梯的级数。

Output

对于每个测试实例,请输出不同走法的数量

Sample Input

2
2
3

Sample Output

1
2
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
using namespace std;
typedef long long ll;
ll a[91]={0,0,1,2,0};
ll yang(ll n)
{
    if(a[n]>0)
       return a[n];
    a[n]=yang(n-1)+yang(n-2);
    return a[n];
}
int main()
{
    ll n,t;
    cin>>t;
    while(t--)
    {
        cin>>n;
        ll ans=yang(n);
        cout<<ans<<endl;
    }
    return 0;
}

B - 算法:汉诺塔

Description

汉 诺塔(又称河内塔)问题是印度的一个古老的传说。开天辟地的神勃拉玛在一个庙里留下了三根金刚石的棒A、B和C,A上面套着n个圆的金片,最大的一个在底 下,其余一个比一个小,依次叠上去,庙里的众僧不倦地把它们一个个地从A棒搬到C棒上,规定可利用中间的一根B棒作为帮助,但每次只能搬一个,而且大的不 能放在小的上面。僧侣们搬得汗流满面,可惜当n很大时这辈子恐怕就很搬了 聪明的你还有计算机帮你完成,你能写一个程序帮助僧侣们完成这辈子的夙愿吗?

Input

输入金片的个数n。这里的n<=10。

Output

输出搬动金片的全过程。格式见样例。

Sample Input

2

Sample Output

Move disk 1 from A to B
Move disk 2 from A to C
Move disk 1 from B to C

Hint

可以用递归算法实现。

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <deque>
using namespace std;
typedef long long ll;
void movee(ll n,ll a,ll b)
{
    char x,y;
    x=a+'A'-1;
    y=b+'A'-1;
    printf("Move disk %lld from %c to %c\n",n,x,y);
}
void yang(ll n,ll a,ll b,ll c)
{
    if(n==1)
    {
        movee(n,a,c);
        return ;
    }
    yang(n-1,a,c,b);
    movee(n,a,c);
    yang(n-1,b,a,c);
}

int main()
{
    ll n;
    cin>>n;
    yang(n,1,2,3);
    return 0;
}

 

posted @ 2018-09-13 11:26  ~~zcy  阅读(389)  评论(1编辑  收藏  举报