Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
29
Task/Combinations/C/combinations-1.c
Normal file
29
Task/Combinations/C/combinations-1.c
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#include <stdio.h>
|
||||
|
||||
/* Type marker stick: using bits to indicate what's chosen. The stick can't
|
||||
* handle more than 32 items, but the idea is there; at worst, use array instead */
|
||||
typedef unsigned long marker;
|
||||
marker one = 1;
|
||||
|
||||
void comb(int pool, int need, marker chosen, int at)
|
||||
{
|
||||
if (pool < need + at) return; /* not enough bits left */
|
||||
|
||||
if (!need) {
|
||||
/* got all we needed; print the thing. if other actions are
|
||||
* desired, we could have passed in a callback function. */
|
||||
for (at = 0; at < pool; at++)
|
||||
if (chosen & (one << at)) printf("%d ", at);
|
||||
printf("\n");
|
||||
return;
|
||||
}
|
||||
/* if we choose the current item, "or" (|) the bit to mark it so. */
|
||||
comb(pool, need - 1, chosen | (one << at), at + 1);
|
||||
comb(pool, need, chosen, at + 1); /* or don't choose it, go to next */
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
comb(5, 3, 0, 0);
|
||||
return 0;
|
||||
}
|
||||
27
Task/Combinations/C/combinations-2.c
Normal file
27
Task/Combinations/C/combinations-2.c
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
#include <stdio.h>
|
||||
|
||||
void comb(int m, int n, unsigned char *c)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < n; i++) c[i] = n - i;
|
||||
|
||||
while (1) {
|
||||
for (i = n; i--;)
|
||||
printf("%d%c", c[i], i ? ' ': '\n');
|
||||
|
||||
/* this check is not strictly necessary, but if m is not close to n,
|
||||
it makes the whole thing quite a bit faster */
|
||||
i = 0;
|
||||
if (c[i]++ < m) continue;
|
||||
|
||||
for (; c[i] >= m - i;) if (++i >= n) return;
|
||||
for (c[i]++; i; i--) c[i-1] = c[i] + 1;
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
unsigned char buf[100];
|
||||
comb(5, 3, buf);
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue