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 maharishi at 2009-05-11 20:27:08 on Problem 1664
#include <iostream>
using namespace std;

int t,M,N;
int result=0;

int f(int m,int n)   //递归式。。
{
	if(m<0)
		return 0;
	if(m==0||n==1)
		return 1;
	
	return f(m-n,n)+f(m,n-1);
}

/****搜索中假设所有的盘子排成一个不增序列***/
void dfs(int depth,int max,int apples)   //用搜索做   这是第depth深度,即第depth个盘子,
{                                        //还有apples个苹果,这个盘子上放max个苹果
	if(depth==N)  //最后一个盘子放置了
	{
		if(apples==0)
		{
			result++;
			return;
		}
		else
		    return;
	}

	for(int i=max;i>=0;i--)
	{
		dfs(depth+1,i,apples-i);
	}
}

int main(int argc, char* argv[])
{
	cin>>t;
	while(t-->0)
	{
		cin>>M>>N;

		for(int k=M;k>=0;k--)
		    dfs(1,k,M-k );   //第一个盘子上放了k个苹果,还剩余M-k个苹果
		cout<<result<<endl;
		result=0;
		//cout<<f(M,N)<<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