第39级台阶

guying1028 2013-05-15 11:17:14
加精
小明刚刚看完电影《第39级台阶》,离开电影院的时候,他数了数礼堂前的台阶数,恰好是39级!

站在台阶前,他突然又想着一个问题:

如果我每一步只能迈上1个或2个台阶。先迈左脚,然后左右交替,最后一步是迈右脚,也就是说一共要走偶数步。那么,上完39级台阶,有多少种不同的上法呢?

请你利用计算机的优势,帮助小明寻找答案。

求源程序和解题思路!
...全文
6787 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
业余草 2015-11-24
  • 打赏
  • 举报
回复
高手在人间
cattpon 2015-11-23
  • 打赏
  • 举报
回复
引用 3 楼 hugett 的回复:
[quote=引用 2 楼 guying1028 的回复:] [quote=引用 1 楼 hugett 的回复:]

#include <stdio.h>

int c[40][2];//0为左脚,1为右脚。。

int main(){
	int i;
	c[0][0] = c[1][1] = 0;
	c[0][1] = c[1][0] = 1;
	for(i = 2; i <= 39; ++i){
		c[i][0] = c[i - 1][1] + c[i - 2][1];
		c[i][1] = c[i - 1][0] + c[i - 2][0];
	}
	printf("%d\n", c[39][1]);
	return 0;
}
可以讲一下你的思想么?我的榆木脑袋理解不了啊。[/quote] 首先你要理解状态c[i][0]表示走到第i个楼梯时最后一步是左脚的方法数,c[i][1]是右脚的方法数。。 那么,由于每一步能上一到两级,c[i][0] = c[i-1][1]+c[i-2][1](因为最后一步为左脚,倒数第二步肯定为右脚。。)。。然后一直递推。。最后c[39][1]即上到第39级而且是右脚的方法数即为答案。。[/quote] 感谢牛人分享~
Enguei_99 2015-11-22
  • 打赏
  • 举报
回复
# include <stdio.h>  
int n = 0;  
int fun(int r, int s)  
{  
    if(r < 0) {  
        return 0;  
    }  
    if(r == 0 && s % 2 == 0) {  
        n++;  
        return 0;  
    }  
    for(int i = 1; i <= 2; i++) {  
        fun(r - i, s + 1);  
    }  
}  
int main(void)  
{  
    fun(39, 0);  
    printf("%d\n", n);  
    return 0;  
}  
lm_whales 2015-11-22
  • 打赏
  • 举报
回复
引用 1 楼 hugett 的回复:

#include <stdio.h>

int c[40][2];//0为左脚,1为右脚。。

int main(){
	int i;
	c[0][0] = c[1][1] = 0;
	c[0][1] = c[1][0] = 1;
	for(i = 2; i <= 39; ++i){
		c[i][0] = c[i - 1][1] + c[i - 2][1];
		c[i][1] = c[i - 1][0] + c[i - 2][0];
	}
	printf("%d\n", c[39][1]);
	return 0;
}
lm_whales 2015-11-22
  • 打赏
  • 举报
回复
注意条件里的偶数步。奇数步的走法要去掉,不符合要求。
白白白小白 2015-11-21
  • 打赏
  • 举报
回复
14楼怎么想出来的 好强大啊
ojc520520 2014-03-22
  • 打赏
  • 举报
回复
没时间想
husterlong 2014-03-21
  • 打赏
  • 举报
回复
#include<stdio.h>
int main(void){
int f[40];
int i;
f[1]=0;
f[2]=1;
f[3]=2;
f[4]=2;
for(i=5;i<40;i++)
f[i]=f[i-2]+f[i-4]+2*f[i-3];
printf("%d",f[39]);
}
孤的代码,不知对不对
赵雨殇 2014-03-20
  • 打赏
  • 举报
回复
好了 第二次bushu不该++。。。。
赵雨殇 2014-03-20
  • 打赏
  • 举报
回复
#include<iostream> #include<stdio.h> #include <stdlib.h> using namespace std; int flag=0; int bushu=0; int main() { int zou(int num); zou(39); cout<<flag<<endl; system("pause"); return 0; } int zou(int num) { if(num==2) flag++; else if(num==1) {bushu=(++bushu)%2;if (bushu==0) flag++;} else { {bushu=(++bushu)%2;zou(num-1);} {bushu=(++bushu)%2;zou(num-2);} } } 亲们看下 我的代码 比上面大哥得出的数字少1....哪里错了
chfchang 2014-03-19
  • 打赏
  • 举报
回复
反正是填空题,直接排列组合做了
iRonRodgeraa 2014-03-18
  • 打赏
  • 举报
回复
思路: * 1.给出条件,先迈左脚,左右交替,最后一步右脚,上39级台阶,迈脚,可能跨1或2级台阶 * 所以,从反方向思考,右脚踏上第39级台阶时,左脚在第38级或第37级,没有其他可能 * 此时的方法总数为,左脚在第38级的方法数加上左脚在第37级的方法数。 * 2.怎么用代码形式表示出这个方法数了? * 定义一个2维数组,1维定义台阶,2维定义左,右脚落在该台阶时的可能数 * int[][] c=new int[40][2]; * 那么,c[0]时,是第一级台阶 * 根据条件,定义c[0][0]=0,左脚落在第零级台阶的可能为0 * 定义c[0][1]=1,右脚落在第零级台阶的可能数为1 * 定义c[1][0]=1,右脚落在第一级台阶的可能数为1 * 定义c[1][1]=0,右脚落在第一级台阶的可能为0 * 得到当左右脚落在台阶上的可能数以后 * c[2][0],表示第二级台阶,左脚落下的可能,此情况只有1种,第一步左脚直接迈两级台阶 * c[2][1],表示第二级台阶,右脚落下的可能,此情况也只有1种,那就是左右脚各迈一级台阶 * * 3.从第三级台阶开始,进入循环 * c[3][0] = c[2][1] + c[1][1] = 1+0=1; * 3台阶左脚 2台阶右脚 1台阶右脚 * * c[3][1] = c[2][0] + c[1][0] =1+1=2; * 3台阶右脚 2台阶左脚 1台阶左脚 * 循环: * for(int i=3;i<=39;i++){ * c[i][0]=c[i-1][1]+c[i-2][1]; * c[i][1]=c[i-1][0]+c[i-2][0]; * } * 结果就为第39级台阶右脚落下的可能数 * c[39][1] 惭愧!!!我是根据你们的代码推出的结论。。。 真心无语,这题主要考察一种程序员思考问题的方法 代码太简单了,可是这种想法,现实中,普通人,根本不会用啊 因为他们不值得for循环啊!!!!!!!! 想哭的心都有了,,,什么奇葩的 程序猿啊!!!!!!!!!!
u011043553 2014-03-09
  • 打赏
  • 举报
回复
引用 1 楼 hugett 的回复:

#include <stdio.h>

int c[40][2];//0为左脚,1为右脚。。

int main(){
int i;
c[0][0] = c[1][1] = 0;
c[0][1] = c[1][0] = 1;
for(i = 2; i <= 39; ++i){
c[i][0] = c[i - 1][1] + c[i - 2][1];
c[i][1] = c[i - 1][0] + c[i - 2][0];
}
printf("%d\n", c[39][1]);
return 0;
}
运行了结果是,你的代码仅仅是提供思路还是
徐刘根 2014-03-09
  • 打赏
  • 举报
回复
[size=16px][b]利用排列组合的思想解答: #include <stdio.h> int c(int m, int n) { int cmn = 1; for(int i = 0; i < n; i++) { cmn = cmn * (m - i) / (i + 1); } return cmn; } int main(void) { int n = 0; for(int i = 1; i <= 19; i += 2) { n += c(39 - i, i); } printf("%d\n", n); return 0; }
aozhi 2013-05-17
  • 打赏
  • 举报
回复

#include <stdio.h>
static int trace[40];
static int count=0;
void go(int, int, int);
int main()
{
        go(39, 0, 1);
        go(39, 0, 2);
        printf("count=%d\n",count);
}
//
// left: left steps
// mt:   mount_times
// step: steps of current movement
//
void go(int left, int mt, int step)
{
        int i;
        if(left < 0)return;
        if(left == 0){
                if(!(mt%2)&&(step==1)){
//下面注释掉的代码用来记录小明的脚步,当然题目里并不要求我们记下来。
                        /*
                        for(i=0;i<mt;i++){
                                printf("%3d",trace[i]);
                        }
                        printf("\n");
                        for(i=mt;i<sizeof(trace);i++){
                                trace[i]=0;
                        }
                        */
                        count++;
                }
                return;
        }
        trace[mt] = step;
        left -= step;
        mt++;
        go(left, mt, 1);
        go(left, mt, 2);
}
derekrose 2013-05-16
  • 打赏
  • 举报
回复
走到第n级台阶的时候,他的上一步不是在n-1级就是在n-2级上, f(n) = f(n - 1) + f(n - 2) 很简单的递归思想
hello_world000 2013-05-16
  • 打赏
  • 举报
回复

int aux(int steps, int cnt)
{
    if (steps <= 0)
    {
        if (0 == steps && (cnt && !(cnt % 2)))
            return 1;
        return 0;
    }
    return aux(steps - 1, cnt + 1) + aux(steps - 2, cnt + 1);
}

inline int func(steps)
{
    return aux(steps, 0);
}
cy2005abc 2013-05-16
  • 打赏
  • 举报
回复
int num=0; void fun(int n,int step) { if(n<0) return; if(n==0) { if(step%2==0) num++; return; } fun(n-1,step+1); fun(n-2,step+1); } void main() { fun(39,0); printf("%d",num); }
hugett 2013-05-15
  • 打赏
  • 举报
回复
引用 2 楼 guying1028 的回复:
[quote=引用 1 楼 hugett 的回复:]

#include <stdio.h>

int c[40][2];//0为左脚,1为右脚。。

int main(){
	int i;
	c[0][0] = c[1][1] = 0;
	c[0][1] = c[1][0] = 1;
	for(i = 2; i <= 39; ++i){
		c[i][0] = c[i - 1][1] + c[i - 2][1];
		c[i][1] = c[i - 1][0] + c[i - 2][0];
	}
	printf("%d\n", c[39][1]);
	return 0;
}
可以讲一下你的思想么?我的榆木脑袋理解不了啊。[/quote] 首先你要理解状态c[i][0]表示走到第i个楼梯时最后一步是左脚的方法数,c[i][1]是右脚的方法数。。 那么,由于每一步能上一到两级,c[i][0] = c[i-1][1]+c[i-2][1](因为最后一步为左脚,倒数第二步肯定为右脚。。)。。然后一直递推。。最后c[39][1]即上到第39级而且是右脚的方法数即为答案。。
guying1028 2013-05-15
  • 打赏
  • 举报
回复
引用 1 楼 hugett 的回复:

#include <stdio.h>

int c[40][2];//0为左脚,1为右脚。。

int main(){
	int i;
	c[0][0] = c[1][1] = 0;
	c[0][1] = c[1][0] = 1;
	for(i = 2; i <= 39; ++i){
		c[i][0] = c[i - 1][1] + c[i - 2][1];
		c[i][1] = c[i - 1][0] + c[i - 2][0];
	}
	printf("%d\n", c[39][1]);
	return 0;
}
可以讲一下你的思想么?我的榆木脑袋理解不了啊。
加载更多回复(1)

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧