我和赵欣苹,秦海龙一起做的一道题:
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.
Sample Input
4 2
10 5
49 6
0 0
Sample Output
6
252
13983816
题目比较好懂(说这话我该脸红,我读了好久啊-_-!),就是编写求解组合的程序。
最后通过的代码如下:
//题目:HEU OJ 1040
//时间:2006/09/03
//参与:牛晋 赵欣苹 秦海龙
#include <stdio.h>
#include <stdlib.h>
int out_of(int,int);
int main()
{
int n,k,result;
while(scanf("%d%d",&n,&k) != EOF){
if(n==0 && k==0) break;
if(k>n/2) k = n-k;
if(k == 0) {
printf("1\n");
continue;
}
if(k == 1) {
printf("%d\n",n);
continue;
}
result=out_of(n, k);
printf("%d\n",result);
}
return 0;
}
int out_of(int n, int k)
{
int n_tmp = n+1-k;
long long result = 1;
for(int k_tmp = 1; k_tmp <= k; k_tmp++){
result *= n_tmp;
//if(result % k_tmp != 0) printf(" Error ");
result /= k_tmp;
++n_tmp;
}
return result;
}
--
Discover the design of nature, discover the evidence of God.