生成可重集的排序 (白书P184)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int ans[6];
int book[6],a[6],n;
void dfs(int cnt)
{
	int i,j;
	if(cnt==n) {
		for(i=0;i<n;i++) 
		if(!i) printf("%d",ans[i]);
		else printf(" %d",ans[i]);
		printf("\n");
		return ;
	}
	for(i=0;i<n;i++) {
		if(book[i]) continue;
	    book[i]=1;
		ans[cnt]=a[i];
		dfs(cnt+1);
		book[i]=0;
		while(i<n&&a[i]==a[i+1]) i++;
	}
}

int main()
{
	while(cin>>n) {
		memset(book,0,sizeof(book));
		memset(ans,0,sizeof(ans));
		for(int i=0;i<n;i++) cin>>a[i];
		dfs(0);
	}
	
}

posted @ 2017-07-17 18:21  yxysuanfa  阅读(140)  评论(0编辑  收藏  举报