1815: The Smart Bunny

 1815: The Smart Bunny


StatusIn/OutTIME LimitMEMORY LimitSubmit TimesSolved UsersJUDGE TYPE
stdin/stdout5s8192K481256Standard

2nd JOJ Cup Online VContest Problem

We all know that bunny is fond of carrots. One cloudy day, he was told that there would be a violenting flood coming soon to destroy the forests. He was scared and after not thinking too much he told himself that he had to escape. He suddenly recalled that there was a temple on the top of the hill and he might shelter there until the flood's past. But unfortunately there was no food for him on the top of the hill, so he had to take his carrots away along with himself. Then he moved to the foot of the hill and stopped. There was only one way for him to get the top of the hill, that is, a long staircase. Given the number of the steps of the staircase, he asked himself:"how many different ways of strides are there for him to get the top of the hill?". Of course, because of his height, he could only stride a limited range of steps. He was smart so much so that he got the answer quickly. Do you know how he did it?

Input Specification

The input consists of several test cases, each of which occupies a line containing M(1<=M<=40) and N(1<=N<=10), where M indicates the number of the steps of the staircase and N indicates the maximal number of steps the bunny can stride once.

Output Specification

Each test case should correspond to a line in the output. Your program should print an integer which is the answer.

Sample Input

4 2
5 4

Sample Output

5
15

 

#include<stdio.h>
int main()
{
 int n,m,i,j;
 while(scanf("%d%d",&m,&n)==2)
 {
  int a[60]={0};
  a[10]=a[11]=1;
  for(i=12;i<60;i++)
   for(j=1;j<=n;j++)
    a[i]+=a[i-j];
  printf("%d/n",a[m+10]);
 }
 return 0;
}

    

/*算法思想:DP解决。arr[i][j] :i 代表剩余阶梯数,j 代表一次可以走的阶梯数,
动态规划为:arr[i][j] = arr[i-1][j] + arr[i-2][j] + ... ... + arr[i-j][j] ,
 当然有些需要特殊处理:arr[0][j] = arr[1][j] = arr[i][1] = 1;
if(j > i) arr[i][j] = arr[i][i] ... ...(*/
#include<iostream>
#define M 41
#define N 11
using namespace std;
int main()
{
       int a[M][N],i,j,t,m,n;
       for(j=0;j<N;j++)
       {
              a[0][j]=1;
              a[1][j]=1;
       }
       for(i=0;i<M;i++)
              a[i][1]=1;
       for(i=2;i<M;i++)
       {
              for(j=2;j<N;j++)
              {
                     if(j>i)
                            a[i][j]=a[i][i];
                     else
                     {
                            a[i][j]=0;
                            for(t=i-1;t>=i-j;t--)
                                   a[i][j]+=a[t][j];
                     }
              }
       }
       while(cin>>m>>n)
              cout<<a[m][n]<<endl;
       return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值