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 |
不看会死人的啊!自认大牛的快看啊!要死人啦!为什么time limit execced!大牛们,请高手指教!不甚感激!#include <stdlib.h> #include <stdio.h> double factorial(int n,int k); main() { int k,n,temp; double result; while(1) { scanf("%d %d",&n,&k); if(!n && !k) break; if(k > n / 2) k = n - k; result = 1; result = factorial(n,k) ; temp = (int)result; printf("%d\n",temp); } } double factorial(int n,int k) { double result = 1,m = 1; int i; if(!n) return 1; for(i = 1;i <= k;i++) { result *= n - i + 1; if(!((int)result % i)) { result /= i; } else { m *= i; } } result /= m; return result ; } 我乘法和除法是同时进行的,对于当次除不尽的用m作临时变量存储最后再除去m,难道还有更省时间的算法?还是我算法有错误?请大牛指教啊!不然真的会死人的啊! Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator