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 37 38 39 40 41 42 43 44 45
| #define MAX_N 10001
int** ans; int ans_size; int temp_size; void findComb(int** ans, int n, int k, int* temp, int i);
int** combine(int n, int k, int* returnSize, int** returnColumnSizes){ ans = malloc(MAX_N * sizeof(int*)); int* tmp = malloc(k * sizeof(int)); ans_size = temp_size = 0; findComb(ans, n, k, tmp, 0); *returnSize = ans_size; *returnColumnSizes = malloc(ans_size * sizeof(int)); for(int i=0; i<ans_size; ++i) { (*returnColumnSizes)[i] = k; } return ans; }
void findComb(int** ans, int n, int k, int* temp, int i) { if (temp_size == k) { int* comb = malloc(k * sizeof(int)); for (int i = 0; i < k; i++) { comb[i] = temp[i]; } ans[ans_size++] = comb; return ; } else if(i == n) { return ; } temp[temp_size++] = i+1; findComb(ans, n, k, temp, i+1); temp_size--; findComb(ans, n, k, temp, i+1); }
|
评论
shortname
for Disqus. Please set it in_config.yml
.