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

Re:救命啊!!!!!实在想不出怎么错啦

Posted by fuwutu at 2005-08-25 09:45:33 on Problem 2249
In Reply To:救命啊!!!!!实在想不出怎么错啦 Posted by:smilezhen at 2005-08-24 19:36:10
兄台的意图应当是防止数据溢出.
C(N,K)=(******)/(******)
如果先让上面的因子全乘起来,再除以下面的,可能会数据溢出.
于是楼主让上面的那些数分别去除下面的数,试图把各因子变小,然后再相乘.
实际上,这种方法很不错.且不论超不超时.你的代码并不能保证把分母里面的那些数全部除去,也就是你最后得出来的数可能比答案要大.


> #include <iostream.h>
> 
> int main(void){
> 	int n, k, *a, *b, sum, i, j;
> 	cin >> n >> k;
> 	while(n!=0 && k!=0){
> 		if(k==0 || k == n) cout << 1<< endl;
> 		else if(k ==1 || k == n-1) cout << n << endl;
> 		else{
> 			if(k>n/2) k = n-k;
> 			a = new int[k];
> 			b = new int[k];
> 			
> 			for(i=0; i<k; ++i){
> 				a[i] = n-i;
> 				b[i] = i+1;
> 			}
> 			
> 			sum = 1;
> 			for(i=0; i<k; ++i){
> 				for(j=k-1; j>0; j--){
> 					if(a[i]%b[j] == 0 && b[j]!=1){
> 						a[i] /= b[j];
> 						b[j] = 1;
> 					}
> 				}
> 				sum *= a[i];
> 				for(j=k-1; j>0; j--){
> 					if(sum%b[j] == 0 && b[j]!=1){
> 						sum /= b[j];
> 						b[j] = 1;
> 					}
> 				}
> 			}
> 			delete []a;
> 			delete []b;
> 
> 			cout << sum << endl;
> 		}
> 		cin >> 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