#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;
}
递归--20层楼梯每次可以走1步2步4步,有几种走法
最新推荐文章于 2022-11-19 20:41:35 发布