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

分别用(int)result和%.0f,result输出,结果为什么不同??

Posted by shenzheng at 2008-02-11 21:18:48 on Problem 2249
先是wa的代码:
#include <stdio.h>

double fun(unsigned long k,unsigned int n)
{
	double result;
	double res=1.0;
	unsigned long i=1;
	if(k==0)  result=1;
	else
	{
	while(i<=k)
	{
		res/=i;
		res*=(n-i+1);
		i++;
	}
	result=res;
	}
	return result;
}

int main()
{
	unsigned long n,k;
	scanf("%ld %ld",&n,&k);
	while(n)
	{
		double result;
		if(k>n/2) result=fun(n-k,n);
		else result=fun(k,n);
		printf("%d\n",(int)result);-----用%d\n,(int)result
	    scanf("%ld %ld",&n,&k);
	}
	return 0;
}

接下来是ac的代码:

#include <stdio.h>

double fun(unsigned long k,unsigned int n)
{
	double result;
	double res=1.0;
	unsigned long i=1;
	if(k==0)  result=1;
	else
	{
	while(i<=k)
	{
		res/=i;
		res*=(n-i+1);
		i++;
	}
	result=res;
	}
	return result;
}

int main()
{
	unsigned long n,k;
	scanf("%ld %ld",&n,&k);
	while(n)
	{
		double result;
		if(k>n/2) result=fun(n-k,n);
		else result=fun(k,n);
		printf("%.0f\n",result);----用的是%.0f\n,result
	    scanf("%ld %ld",&n,&k);
	}
	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