Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Re:用递归,这道题超时啊,怎么解决,求助~~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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator