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 |
前者取整数部分,后者四舍五入In Reply To:分别用(int)result和%.0f,result输出,结果为什么不同?? Posted by:shenzheng at 2008-02-11 21:18:48 > 先是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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator