-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCombination.hpp
More file actions
36 lines (29 loc) · 812 Bytes
/
Copy pathCombination.hpp
File metadata and controls
36 lines (29 loc) · 812 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#ifndef ASU_COMBINATION
#define ASU_COMBINATION
#include<iostream>
/**************************************************************
* This C++ template returns the combination of input integers.
*
* input(s):
* const std::size_t &n ---- Lower number.
* const std::size_t &k ---- Upper number.
*
* return(s):
* int ans ---- C_n^k
*
* Shule Yu
* Mar 10 2015
*
* Key words: combination
**************************************************************/
int Combination(const std::size_t &n, const std::size_t &k){
if ( n < 0 || k < 0 || k > n ){
std::cerr << "Error in " << __func__ << ": mathematic error ..." << std::endl;
return 0;
}
int ans=1;
for (std::size_t i=1;i<=k;++i) ans*=(n-k+i);
for (std::size_t i=1;i<=k;++i) ans/=i;
return ans;
}
#endif