爬楼梯问题,输出总数以及过程
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();
}
#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();
}
相关文章推荐
- Makefile中echo -e 以及 输出$()和%.o 这两个字符串的问题
- 在 Sublime Text 2 中编译和运行 Java 程序,以及输出中文出错问题解决办法
- Spring 整合redis集群 实现 以及过程中需要注意的问题点
- Webstrom 使用过程中遇到的问题以及解决方案
- Ubuntu 12.04(32位)安装Oracle 11g(32位)全过程以及几乎所有问题的解决办法
- 关于sqlhelper调用存储过程获取参数返回值和输出参数的问题
- 在写代码过程中遇到的问题,以及当时的解决方法(如实记录)
- Ubutun使用过程中遇到的问题以及解决方案
- 数据库的几个问题存储过程触发器函数创建以及sql优化
- DNN训练过程中的一些问题以及技巧
- caffe的搭建过程以及遇上的各种问题的汇总
- Mybatis 的Log4j日志输出问题 - 以及有关日志的所有问题
- tomcat7源码编译过程以及问题解决
- 安装MongoDB的方法以及安装过程中遇到的问题
- 编写程序,统计某旅馆住宿客人的总数,要求输入客人姓名,输出客人编号(按先后顺序自动生成),姓名以及总人数。
- 你所不知道的SQL Server数据库启动过程,以及启动不起来的各种问题的分析及解决技巧
- 第一个MYSQL存储过程以及其中遇到的空格问题
- eclipse安装activiti5.18.0工作流插件 以及安装过程中activiti插件出现的问题及解决
- Git 使用过程中遇到的问题以及解决办法
- 使用RabbitMQ过程中遇到的一个问题(队列为空,但内存暴涨)以及与开发者的邮件沟通