#include<string.h>
#include<iostream>
#include<stack>
using namespace std
int main()
{
int n = 0, m = 0, sum = 0, cnt = 0
cout <<"输入明亏闹阶梯数:" <激罩<endl
cin >>n
cout <<"输入最多爬的阶梯数:" <<endl
cin >>m
if (n <= 0)
{
cout <<"就一种,上去了!" <<endl
}
if (m <= 0)
{
cout <<"你想上是不可能的!" <<endl
}
int i
stack<int>sk
do
{
if (sum <= n)
{
++cnt
while (sum <= n)
{
sk.push(1)
++sum
}
}
if (!sk.empty())
{
sum -= sk.top()
sk.pop()
}
else
{
cout <<cnt <<endl
return 0
}
HHH:
if (!sk.empty())
{
i = sk.top()
}
else
{
cout <<cnt <<endl
return 0
}
if (i <m)
{
++i
}
else
{
if (!sk.empty())
{
sum -= sk.top()
sk.pop()
goto HHH
}
else
{
cout <<cnt <<endl
return 0
}
}
if (!sk.empty())
{
sum -= sk.top()
sk.pop()
}
else
{
cout <<cnt <<endl
return 0
}
sk.push(i)
sum += i
} while (1)
return 0
}
一定要用递归的就这样写:public class Test{
static final int s = 10//自碧乎茄定义的台阶数
static int len = 0, sum = 0
static int step[] = new int[s]
static void compute(final int stair) {
if(stair<0) return
if(stair==0) {
printSum()
sum++
return
}
for(int i = 1i <= 3i++) {
step[len] = i
len++
compute(stair-i)
len--
}
}
static void printSum() {
System.out.print("走法:")
for(int i = 0i <leni++)
System.out.print(step[i]+ "悔察 ")
System.out.println()
}
public static void main(String args[]){
compute(s)
System.out.println("共有" + sum + "种走法顷雹")
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)