您的位置:首页 > 其它

爬楼梯问题,输出总数以及过程

2018-01-12 19:25 211 查看
#include <iostream>

#include <stack>

#define MAX_STEP 15

bool nextStep(int step, int totalStep, unsigned long long &count, std::stack<int> &steps)

{
steps.push(step);
if (totalStep + step == MAX_STEP)
{
std::stack<int> temp;
while (!steps.empty())
{
std::cout << steps.top() << " ";
temp.push(steps.top());
steps.pop();
}
while (!temp.empty())
{
steps.push(temp.top());
temp.pop();
}
//std::cout << steps.size() << std::endl;
std::cout << std::endl;
count++;
return true;
}
else if (totalStep + step > MAX_STEP)
{
return false;
}
else
{
totalStep += step;
nextStep(1, totalStep, count, steps);
steps.pop();
nextStep(2, totalStep, count, steps);
steps.pop();

}

}

int main()

{
std::stack<int> steps;
unsigned long long count = 0;
nextStep(1, 0, count, steps);
steps.pop();
nextStep(2, 0, count, steps);
steps.pop();
std::cout << "all count" << count << std::endl;
std::getchar();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐