用递归函数或使用堆栈生成排列组合数

#include <iostream>
using namespace std;
#define MAXN 10			/* 栈的最大容量 */
/* 定义栈的类型为int */
typedef int ElemType;
typedef struct
{
    
    
	ElemType data[MAXN];
	int top;
}SqStack;
void InitStack(SqStack &st)//初始化栈
{
    
    
	st.top = -1;
}
void  DestroyStack(SqStack &st)//销毁栈
{
    
    
}
int push(SqStack &st,ElemType &x)	/* 进栈函数 */
{
    
    
	if (st.top == MAXN - 1)
		return 0;
	else 
	{
    
    
		st.top++;
		st.data[st.top] = x;
		return 1;
	}
}
int pop(SqStack &st, ElemType &x)		/*出栈函数*/
{
    
    
	if (st.top == -1)
		return 0;
	else {
    
    
		x = st.data[st.top];
		st.top--;
		return 1;
	}
}

int EmptyStack(SqStack &st)//判断栈是否为空
{
    
    
	if (st.top == -1)
		return 0;
	else return 1;
}
int GetTop(SqStack &st, ElemType &x)//获取栈顶元素
{
    
    
	if (st.top == -1)
		return 0;
	else 
	{
    
    x = st.data[st.top];
	return 1;
	}
	
}

void OutputStack(SqStack st)	/* 输出栈元素 */
{
    
    
	for (;st.top > -1; st.top--)
		cout <<st.data[st.top] << "  ";
}
int zhs(SqStack &st,int sz[],int i)//{A, B, C, D, E}中任取三个元素的组合数
{
    
    
	int x1, x2, x3;
	pop(st, x1);
	pop(st, x2);
	pop(st, x3);
	cout << x1 << " " << x2 << " " << x3 << endl;
	if (!EmptyStack(st))
	{
    
    
		i--;
		for(int j=0;j<i;j++)
		push(st, sz[j]);
	if (i < 3)
		return 0;
	}
	else
	{
    
    
		push(st, x2);
		push(st, x1);
	}
	zhs(st, sz,i);
}
#include "标头1.h"
int main()
{
    
    
	SqStack st;
	InitStack(st);
	int sz[5];
	cout << "请输入分别5个数:";
	cin >> sz[0] >> sz[1] >> sz[2] >> sz[3]>> sz[4];
	push(st, sz[0]);
	push(st, sz[1]);
	push(st, sz[2]);
	push(st, sz[3]);
	push(st, sz[4]);
	zhs(st, sz,5);//{A, B, C, D, E}中任取三个元素的组合数
	system("pause");
}
2)
#include <iostream>
using namespace std;
#define MAXN 10			/* 栈的最大容量 */
/* 定义栈的类型为int */
typedef int ElemType;
typedef struct
{
    
    
	ElemType data[MAXN];
	int top;
}SqStack;
void InitStack(SqStack &st)//初始化栈
{
    
    
	st.top = -1;
}
void  DestroyStack(SqStack &st)//销毁栈
{
    
    
}
int push(SqStack &st, ElemType &x)	/* 进栈函数 */
{
    
    
	if (st.top == MAXN - 1)
		return 0;
	else
	{
    
    
		st.top++;
		st.data[st.top] = x;
		return 1;
	}
}
int pop(SqStack &st, ElemType &x)		/*出栈函数*/
{
    
    
	if (st.top == -1)
		return 0;
	else {
    
    
		x = st.data[st.top];
		st.top--;
		return 1;
	}
}

int EmptyStack(SqStack &st)//判断栈是否为空
{
    
    
	if (st.top == -1)
		return 0;
	else return 1;
}
int GetTop(SqStack &st, ElemType &x)//获取栈顶元素
{
    
    
	if (st.top == -1)
		return 0;
	else
	{
    
    
		x = st.data[st.top];
		return 1;
	}

}

void OutputStack(SqStack st)	/* 输出栈元素 */
{
    
    
	for (; st.top > -1; st.top--)
		cout << st.data[st.top] << "  ";
}

void dzhs1(SqStack &st, int sz[])//{A, B, C, D, E}中任取三个元素的组合数
{
    
    
	int x1, x2, x3,x4,x5;
	pop(st, x1);
	pop(st, x2);
	pop(st, x3);
	pop(st, x4);
	pop(st, x5);
	cout << x1 << endl;
	cout << x2 << endl;
	cout << x3 << endl;
	cout << x4 << endl;
	cout << x5<< endl;
}
int dzhs2(SqStack &st, int sz[], int i)//{A, B, C, D, E}中任取三个元素的组合数
{
    
    
	int x1, x2;
	pop(st, x1);
	pop(st, x2);
	cout << x1 << " " << x2 <<endl;
	if (!EmptyStack(st))
	{
    
    
		i--;
		for (int j = 0; j < i; j++)
			push(st, sz[j]);
		if (i < 2)
			return 0;
	}
	else
	{
    
    
		push(st, x1);
	}
	dzhs2(st, sz, i);
}

int dzhs3(SqStack &st, int sz[], int i)//{A, B, C, D, E}中任取三个元素的组合数
{
    
    
	int x1, x2, x3;
	pop(st, x1);
	pop(st, x2);
	pop(st, x3);
	cout << x1 << " " << x2 << " " << x3 << endl;
	if (!EmptyStack(st))
	{
    
    
		i--;
		for (int j = 0; j < i; j++)
			push(st, sz[j]);
		if (i < 3)
			return 0;
	}
	else
	{
    
    
		push(st, x2);
		push(st, x1);
	}
	dzhs3(st, sz, i);
}
#include"标头.h"
int main()
{
    
    
	SqStack st1;
	InitStack(st1);
	SqStack st2;
	InitStack(st2);
	SqStack st3;
	InitStack(st3);
	int sz[5];
	cout << "请输入分别5个数:";
	for (int i = 0; i < 5; i++)
		cin >> sz[i];
	push(st1, sz[0]); push(st1, sz[1]); push(st1, sz[2]); push(st1, sz[3]); push(st1, sz[4]);
	push(st2, sz[0]); push(st2, sz[1]); push(st2, sz[2]); push(st2, sz[3]); push(st2, sz[4]);
	push(st3, sz[0]); push(st3, sz[1]); push(st3, sz[2]); push(st3, sz[3]); push(st3, sz[4]);
	dzhs1(st1, sz);
	dzhs2(st2, sz, 5);
	dzhs3(st3, sz, 5);
	system("pause");
}
3)
#include <iostream>
using namespace std;
void pl(int start, int end, int a[])
{
    
    
	if (start == end) {
    
    
		for (int i = 0; i < end; i++)
			cout << a[i] << ' ';
		cout << endl;
		return;
	}
	for (int i = start; i < end; i++)
	{
    
    
		swap(a[start], a[i]);      
		pl(start + 1, end, a);   
		swap(a[i], a[start]);      
	}
}
#include"标头.h"
int main()
{
    
    
	int sz[5];
	cout << "请输入分别5个数:";
	cin >> sz[0] >> sz[1] >> sz[2] >> sz[3] >> sz[4];
	pl(0, 5, sz);
	system("pause");
}

猜你喜欢

转载自blog.csdn.net/weixin_43791941/article/details/100176647
今日推荐