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

Re:用递归,这道题超时啊,怎么解决,求助~~

Posted by 806601756 at 2011-07-26 10:56:16 on Problem 1953
In Reply To:Re:用递归,这道题超时啊,怎么解决,求助~~ Posted by:806601756 at 2011-07-26 10:55:23
用一个数组记录已经算过的,下次就不用在算了,直接取就行。。。
#include <stdio.h>
#include <memory.h>

__int64 v[50][50];

__int64 c(int m,int n)
{
	if(n==0||m==n)return 1;
	else if(n==1)return m;
	else 
	{
		if(v[m][n]==-1)
			v[m][n]=c(m-1,n)+c(m-1,n-1);
		return v[m][n];
	}
}

int main()
{
	int cases ,n,a,b,i,w=0;
	__int64 sum=0;
	scanf("%d",&cases);
	while (cases--)
	{
		w++;
		memset(v,-1,sizeof(v));
		sum=0;
		scanf("%d",&n);
		a=(int)((n+1)/2);
		for(i=0;i<=a;i++)
		{
			b=n-i+1;
			sum=sum+c(b,i);
		}
		printf("Scenario #%d:\n",w);
		printf("%I64d\n",sum);
		if(cases>0)printf("\n");
	}
	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