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

为何会WA呢?大惑不解

Posted by speedfirst at 2007-01-31 21:31:59 on Problem 1306
#include <stdio.h>


int gcd(int a, int b)
{
	int i = a % b;
	if(i == 0)
	{
		return b;
	}
	else
	{
		return gcd(b, i);
	}
}

void dividebygcd(long &a, long &b)
{
	long gcd_value = gcd(a, b);
	a /= gcd_value;
	b /= gcd_value;
}

long combination(int m, int n)
{
	long denominator = 1;
	long numerator = 1;
	long multi = 1;
	long divide = 1;

	if(m > (n / 2))
	{
		m = n - m;
	}

	if(m == 0)
	{
		return 1;
	}

	while(m != 0)
	{
		multi = n--;
		divide = m--;
		dividebygcd(multi, divide);
		dividebygcd(multi, denominator);
		dividebygcd(divide, numerator);
		denominator *= divide;
		numerator *= multi;		
	}
	return (numerator / denominator);	
}

int main(int argc, char * argv[])
{
	int m, n;
	do
	{
		scanf("%d", &n);
		scanf("%d", &m);
		if(m == 0 && n == 0)
		{
			return 0;
		}
		printf("%d things taken %d at a time is %d exactly\n",n, m, combination(m, n) );
	}while(1);
	
}


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