-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathp077.c
62 lines (58 loc) · 1.39 KB
/
p077.c
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *columnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/
int *tmp;
int **result;
void f(const int n, const int k, int *index, const int pos, const int val)
{
if (pos == k)
{
for (int i = 0; i < k; i++)
{
result[*index][i] = tmp[i];
}
(*index)++;
return;
}
for (int i = val; i <= n-k+pos+1; i++)
{
tmp[pos] = i;
f(n, k, index, pos+1, i+1);
}
}
int** combine(int n, int k, int** columnSizes, int* returnSize) {
if (n == 0)
{
(*returnSize) = 0;
(*columnSizes) = NULL;
return NULL;
}
(*returnSize) = 1;
int i, j;
for (i = 1; i <= k; i++)
(*returnSize) = (*returnSize)*(n-k+i)/i;
result = (int**)malloc(sizeof(int*)*(*returnSize));
(*columnSizes) = (int*)malloc(sizeof(int)*(*returnSize));
if (k == 0)
{
for (i = 0; i < *returnSize; i++)
{
(*columnSizes)[i] = 0;
}
return result;
}
else
{
for (i = 0; i < *returnSize; i++)
{
(*columnSizes)[i] = k;
result[i] = (int*)malloc(sizeof(int)*k);
}
}
tmp = (int*)malloc(sizeof(int)*k);
i = 0;
f(n, k, &i, 0, 1);
return result;
}