Skip to content

Instantly share code, notes, and snippets.

@louisswarren
Created September 19, 2020 23:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save louisswarren/ad24db6ee1ec8033d66e3636ad2680da to your computer and use it in GitHub Desktop.
Save louisswarren/ad24db6ee1ec8033d66e3636ad2680da to your computer and use it in GitHub Desktop.
Find non-intersecting binary triplets
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
void print_bin(uint32_t x)
{
int i;
for (i = 31; i >= 0; --i)
putchar(x & (1 << i) ? '1' : '0');
}
int
main(void)
{
size_t n;
uint32_t *v;
size_t i, j, k;
int c;
char buf[32];
if (!scanf("%zu\n", &n)) {
fprintf(stderr, "Could not read input size\n");
return 1;
} else if ((v = calloc(n, sizeof(*v))) == NULL) {
fprintf(stderr, "Not enough memory\n");
return 1;
}
for (i = 0; i < n;) {
switch (c = getchar()) {
case '0':
v[i] = v[i] << 1;
break;
case '1':
v[i] = (v[i] << 1) + 1;
break;
case EOF:
fprintf(stderr, "Unexpected EOF\n");
return 1;
default:
++i;
}
}
for (i = 0 + 0; i < n; ++i) {
for (j = i + 1; j < n; ++j) {
for (k = j + 1; k < n; ++k) {
if (!(v[i] & v[j] & v[k])) {
print_bin(v[i]);
putchar(' ');
print_bin(v[j]);
putchar(' ');
print_bin(v[k]);
putchar('\n');
}
}}}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment