C++ジョブ03_02:階下問題.上の階から下の階まで歩くとhつの階段があり、一歩ごとに3つの歩き方があります.

1281 ワード

/*
  03,   2

    。         h   ,        :
 1   , 2   , 3   。         。       。

*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

static int stack[1024];       //          
static int steps = 0;         //      
static int num_of_method = 0; //      

void NextStairs(int n)
{
	if(n == 0)
	{
		/*       ,       ,         */
		printf(" %3d   [ %3d ] : ", ++num_of_method, steps);
		int i;
		for(i=0; i<steps; i++)
			printf("%d ", stack[i]);
		printf("
"); return; } if(n >= 1) { stack[steps++] = 1; // 1 NextStairs(n-1); steps --; } if(n >= 2) { stack[steps++] = 2; // 2 NextStairs(n-2); steps --; } if(n >= 3) { stack[steps++] = 3; // 3 NextStairs(n-3); steps --; } } int ex03_02() { int n; printf("enter a positive number: n="); scanf("%d", &n); NextStairs(n); return 0; }