哈理工OJ 1124 Final Destination(简单递推)

Final Destination
Time Limit: 1000 MS Memory Limit: 65536 K
Total Submit: 599(188 users) Total Accepted: 195(175 users) Rating: Special Judge: No
Description
JiaoZhu likes going on adventure! One day, he walks into a big castle, and there is an unique stairway. JiaoZhu finds a board ,it says “The one who want to go upstairs only can go three steps the most once, meaning that you can go 1 or 2 or 3 steps once!”. Now, we have a problem, can you tell me the number of ways to go to the destination? If you can’t ,death is the only choice。
In the beginning, you are in the 0th step.
Input
First input a integer T(T<50), represent the number of case.

Each case ,the input will consist only a positive integer n (0<=n<=30), represent the nth steps you want to go to..

Output
Order the sample output format to output.

Line 1,print the Case k.

Line 2,print one integer represent the number of ways to go to nth steps.

Sample Input
2
1
2
Sample Output
Case 1:
1
Case 2:
2
Hint
When n=2,you can go one step once to go to 2th ,or go 2 steps once to 2th,so the answer is 2.
一道简单的递推题目,有个坑点,dp[0]=1;
递推过程,当目的地大于3的时候,他前面的三个点都能直接到达这个点。
所以可以得到以下的递推关系式:
i=0;dp[0]=1;
i=1;dp[1]=1;
i=2;dp[2]=2;
i=3;dp[3]=4
i>3时
dp[i]=dp[i-1]+dp[i-2]+dp[i-3]
下面是AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

int fib[45];
int main()
{
    fib[0]=1,fib[1]=1,fib[2]=2,fib[3]=4;;
    for(int i=4;i<40;i++)
    {
        fib[i]=fib[i-1]+fib[i-2]+fib[i-3];
    }
    int t,n,iCase=0;
    scanf("%d",&t);
    while(t--)
    {
        iCase++;
        scanf("%d",&n);
        printf("Case %d:\n",iCase);
        printf("%d\n",fib[n]);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值