| ||||||||||
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:这是我的源代码,但是运行超时,请高手帮忙改一下。 Posted by:huatao at 2008-03-23 10:17:19 > #include <iostream> > #include <math.h> > using namespace std; > > long factorial(int n) > { > int i; > long result = 1; > for (i=1; i<=n; i++) > result *= i; > return result; > } > > long C(int n, int m) > { > return factorial(n) / factorial(m) / factorial(n-m); > } > > long f(int n) > { > if (n==1 || n==2) > return 1; > if (n==3) > return 4; > int m = C(n, 2); > int p = pow(2, m); > long result = p; > int i; > for (i=1; i<n; i++) //递归 > { > result -= C(n, i)*f(i); > } > return result; > } > > int main() > { > int n; > cin>>n; > long result; > while (n!=0) > { > result = f(n); > cout<<result<<endl; > cin>>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