조합 개수 POJ2249 및 조합 출력 방법 구하기

Description
In how many ways can you choose k elements out of n elements, not taking order into account? 
Write a program to compute this number.
Input
The input will contain one or more test cases. 
Each test case consists of one line containing two integers n (n>=1) and k (0<=k<=n). 
Input is terminated by two zeroes for n and k.
Output
For each test case, print one line containing the required number. This number will always fit into an integer, i.e. it will be less than 2
31. 
Warning: Don't underestimate the problem. The result will fit into an integer - but if all intermediate results arising during the computation will also fit into an integer depends on your algorithm. The test cases will go to the limit. 
Sample Input
4 2
10 5
49 6
0 0

Sample Output
6
252
13983816
 , , 
 
#include 
#include 
#include 
#include 

using namespace std;

int main()
{
	long long a,b,i,sum1,sum2;
	//freopen("E://input.txt","r",stdin);
	scanf("%lld%lld",&a,&b);
	while(!(a==0&&b==0))
	{
		sum1=sum2=1;
		if(b>a-b)
            b=a-b;  // , TLE 0ms
		for (i=a;i>a-b;i--)
		{
			sum1*=i;
		    sum1/=sum2;
			sum2++;
		}
		printf("%lld/n",sum1);
		scanf("%lld%lld",&a,&b);
	}
	return 0;
}
 
#include 
#include 
#include 
#include 

using namespace std;

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

int main()
{
	//freopen("E://input.txt","r",stdin);
    int i,j,g,k,n,u[40];
    while(scanf("%d%d",&n,&k),n) {
        if(k>(n/2)) k=n-k;
        for(i=0;i1);i++) {
                g=gcd(u[i],n);
                u[i]/=g;
                n/=g;
            }
        }
        n=1;
        for(i=0;i 
  

 

N M

 

#include #include #include #include using namespace std; int num,ResMatrix[15]; void GetRes(int s,int d) { int i,j; for (i=s;i>=d;i--) { ResMatrix[d]=i; if (d==1) { for (j=num;j>=1;j--) printf("%d",ResMatrix[j]); printf("/n"); } else GetRes(i-1,d-1); } } int main() { int n; scanf("%d%d",&n,&num); GetRes(n,num); return 0; }  

좋은 웹페이지 즐겨찾기