📄 车厢调度2.3ywc.cpp
字号:
#include<stdio.h>
#include<iostream.h>
#define MaxLen 100
struct snode{
int data[MaxLen];
int top;
}s;//定义一个栈指针
int n;//定义输入序列总个数
void Initstack()
{
s.top=-1;
}
void push(int q)//元素n进栈
{
s.top++;
s.data[s.top]=q;
}
int pop()//出栈
{
int temp;
temp=s.data[s.top];
s.top--;
return temp;
}
int Emptys()//判断栈空
{
if(s.top==-1)
return 1;
else
return 0;
}
void process(int pos,int path[],int curp)//当前处理位置pos的元素
{
int m,i;
if(pos<n)//编号进栈递归
{
push(pos+1);
process(pos+1,path,curp);
pop();
}
if(!Emptys())//编号出栈递归
{
m=pop();
path[curp]=m;
curp++;
process(pos,path,curp);
push(m);
}
if(pos==n&&Emptys())//输出一种可能的方案
{
for(i=0;i<curp;i++)
cout<<path[i]<<" ";
cout<<endl;
}
}
void main()
{
int path[MaxLen];
cout<<"输入要调度车厢总数:";
cin>>n;
Initstack();
push(1);
cout<<"所有输出序列:"<<endl;
process(1,path,0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -