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

不看会死人的啊!自认大牛的快看啊!要死人啦!为什么time limit execced!大牛们,请高手指教!不甚感激!

Posted by lzj_kkx at 2009-08-30 20:34:12 on Problem 2249
#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:
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