您的位置:首页 > 其它

递归--20层楼梯每次可以走1步2步4步,有几种走法

2012-10-31 02:30 281 查看
#include <stdio.h>
#include <stdlib.h>

#define SIZE_STACK 100
int sum = 0;
int final_floor = 20;
int step[3] = {1,2,4};
int count = 0;
int push_count = 0;
int top = -1;
int stack[SIZE_STACK];

void push(int n)
{
if(top + 1 == SIZE_STACK)
{
fprintf(stderr,"%s\n","Stack FUll!");
return;
}

stack[++top] = n;
}

int pop()
{
if(top == -1)
{
fprintf(stderr,"%s\n","Stack Empty!");
return -1;
}

return stack[top--];
}

void disStack()
{
++ count;
for(int i = 0; i <= top; ++i)
{
printf("%d ",stack[i]);
}

printf("\n");
}

void get_step()
{
if(sum == 20)
{
disStack();
return;
}
for(int i = 0; i < 3; ++i)
{
if(sum + step[i] <= 20)
{
push(step[i]);
sum += step[i];
get_step();
pop();
sum -= step[i];
}
}
}

int main()
{
get_step();

printf("\n%d ways to go to 20 stairs.\n",count);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐