一个8级的台阶,一个人一步最多可以跨3级,一共有多少方法走完?
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
//一个8级的台阶,一个人一步最多可以跨3级,一共有多少方法走完
#define RANK 8
#define STEP 3
void count(int *arr,int end,int rank,int step,int *num)
{
for(int i = 1;i<=step;i++)
{
if((rank - i)==0)
{
(*num)++;
arr[end] = i;
cout<<setw(3)<<*num<<"---";
for(int k=0;k<=end;k++)
cout<<setw(3)<<arr[k];
cout<<endl;
}
else if((rank-i)>0)
{
arr[end]=i;
count(arr,end+1,rank-i,step,num);
}
}
}
void main()
{
int num = 0;
int arr[RANK];
count(arr,0,RANK,STEP,&num);
cout<<"num = "<<num<<endl;
system("pause");
}
输出: