1. 程式人生 > >生成可重集的排序 (白書P184)

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

print ems while using continue popu names ++ content

#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);
	}
	
}

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