您的位置:首页 > 其它

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

2016-05-15 21:43 357 查看
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
);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: