Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

晒晒我的方法

Posted by Snow_storm at 2010-02-20 16:06:21 on Problem 2245
回溯枚举每个位置可能的排列组合
并按字典序生成  注意最后一位不能大于k

#include<iostream>
using namespace std;
int f[14];
int s[14];
int k;
void retion(int n,int m,int l)
{
	int i;
	if(!n && s[6]<=k)
	{
		for(i=1;i<6;i++)
			cout<<f[s[i]]<<" ";
		cout<<f[s[6]]<<endl;
	}
	else if(n)
	{
		for(i=m+1;i<=l/n;i++)
		{
			s[6-n+1]=i;
			retion(n-1,i,l-i);
		}
	}
}
int main()
{
	int i;
	while(1)
	{
		cin>>k;
		if(k==0) break;
		for(i=1;i<=k;i++)
			cin>>f[i];
		retion(6,0,k*(k+1)/2);
		cout<<endl;
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator