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

#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");
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值