| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1112 人关注过本帖
标题:出栈序列中的递归问题
取消只看楼主 加入收藏
大葱
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2007-12-21
收藏
 问题点数:0 回复次数:2 
出栈序列中的递归问题
下面是关于出栈序列的问提,其中//??????中间的那个递归应用还是不太懂,那位解释一下,感激不尽!
#include <iostream>
using namespace std;

const int n = 4;

struct Queue {
    
    int data;
    int index;
};

class StackQueue {
    
private:
    int number;
public:
    StackQueue();
    ~StackQueue();
    int getNumber();
    void all_Queue(int step,int* save,struct Queue* pop,int last);
};

StackQueue :: StackQueue() {

    number = 0;
}

StackQueue :: ~StackQueue() {}
 
void StackQueue :: all_Queue(int step,int* save,struct Queue* pop,int last) {
    
    if(step == n) {
        
        for(int i=0;i < n;i++)
            cout << pop[i].data << " ";
        cout << endl;
        number++;
        return;
    }
    int flag = 0,count = 0;
 //?????????????????????????????
    for(int j=0;j < n;j++) {
        
        for(int k=0;k < step;k++)
            if(j == pop[k].index)  flag = 1;
            
        if(flag == 1) {
            
            flag = 0;
            continue;
        }

        if(step == 0) last = j;
        int temp_last = last;
        for(temp_last--;temp_last > j;temp_last--) {
            for(k=0;k < step;k++)
                if(temp_last == pop[k].index)
                    count++;
        }
        
        if(count < last-j-1) {
                
            count = 0;
            continue;
        }
        pop[step].data = save[j];
        pop[step].index = j;
        all_Queue(step+1,save,pop,j);//递归语句
    }
//??????????????????????????????????????///
}

int StackQueue ::getNumber() {
    
    return number;
}

int main() {

    int save[n] = {1,2,3,4};
    struct Queue pop[n];
    StackQueue stackQueue;
    stackQueue.all_Queue(0,save,pop,-1);
    cout << stackQueue.getNumber() << endl;

    return 0;
}
搜索更多相关主题的帖子: 栈序列 int 递归 StackQueue void 
2008-04-20 16:43
大葱
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2007-12-21
收藏
得分:0 
C++标准里的么?我这搜到一个,但没有用到递归啊
//C++ 标准模板库(STL)编程示例 - 容器适配器Stack
#include <iostream>
#include <assert.h>
#include <vector>
#include <deque>
#include <list>
#include <stack>
#include <string>
using namespace std;
struct employee
{
//Member Function
public:
 employee(long eID, string e_Name, float e_Salary);
//Attribute
public:
 long ID;                  //Employee ID
 string name;              //Employee Name
 float salary;           //Employee Salary
};
//员工类构造函数
employee::employee(long eID, string e_Name, float e_Salary)
: ID(eID), name(e_Name), salary(e_Salary) {}
//定义一个用Vector维护被控序列的栈类型
typedef stack<employee, vector<employee, allocator<employee> > > V_EMPLOYEE_STACK;
//定义一个用Deque维护被控序列的栈类型
typedef stack<employee, deque<employee, allocator<employee> > > D_EMPLOYEE_STACK;
//定义一个用List维护被控序列的栈类型
typedef stack<employee, list<employee, allocator<employee> > > L_EMPLOYEE_STACK;
         
int main(int argc, char* argv[])
{
 V_EMPLOYEE_STACK v_employee;
 D_EMPLOYEE_STACK d_employee;
 L_EMPLOYEE_STACK l_employee;
 //下面的三条语句分别构造三个employee对象,然后入容器对象为Vector的栈
 v_employee.push(V_EMPLOYEE_STACK::value_type(100, "huahua", 200000));
 v_employee.push(V_EMPLOYEE_STACK::value_type(101, "jiafeng", 8000));
 v_employee.push(V_EMPLOYEE_STACK::value_type(101, "unknown", 20000));
    assert(!v_employee.empty());
 //下面这条语句弹出最后入栈的employee对象
 v_employee.pop();
 //下面的三条语句分别构造三个employee对象,然后入容器对象为Deque的栈
 d_employee.push(D_EMPLOYEE_STACK::value_type(100, "huahua", 200000));
 d_employee.push(D_EMPLOYEE_STACK::value_type(101, "jiafeng", 8000));
 d_employee.push(D_EMPLOYEE_STACK::value_type(101, "unknown", 20000));
 assert(!d_employee.empty());
 //下面这条语句弹出最后入栈的employee对象
 d_employee.pop();
 //下面的三条语句分别构造三个employee对象,然后入容器对象为List的栈
 l_employee.push(L_EMPLOYEE_STACK::value_type(100, "huahua", 200000));
 l_employee.push(L_EMPLOYEE_STACK::value_type(101, "jiafeng", 8000));
 l_employee.push(L_EMPLOYEE_STACK::value_type(101, "unknown", 20000));
 assert(!l_employee.empty());
 //下面这条语句弹出最后入栈的employee对象
 l_employee.pop();
 cout << "容器对象为Vector的栈是否为空? " << (v_employee.empty() ? "TRUE" : "FALSE") << endl;
 cout << "容器对象为Vector的栈共有" << v_employee.size() << "个employee对象!" << endl;
 cout << "容器对象为Deque的栈是否为空?" << (d_employee.empty() ? "TRUE" : "FALSE") << endl;
 cout << "容器对象为Deque的栈共有" << d_employee.size() << "个employee对象!" << endl;
 cout << "容器对象为List的栈是否为空?" << (l_employee.empty() ? "TRUE" : "FALSE") << endl;
 cout << "容器对象为List的栈共有" << l_employee.size() << "个employee对象!" << endl;
 return 0;
}
问题中那程序的递归似乎有点复杂,我用调试一步步看,但还没看懂,能简单的讲解一下么,谢谢了
2008-04-20 17:01
大葱
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2007-12-21
收藏
得分:0 
怎么构造?递归构造么?我手上只有Thiking in C++《C++编程思想》和潭浩强的那本,有你说的那些么?呵呵
2008-04-20 17:18
快速回复:出栈序列中的递归问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.041527 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved