Created
November 26, 2016 01:57
-
-
Save gkastrinis/5519f825b4e9ef5e857fde21f600a756 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdint.h> | |
#include <string.h> | |
void magic(char* str, int i, int changesLeft) { | |
if (changesLeft == 0) { | |
printf("%s\n", str); | |
return; | |
} | |
if (i < 0) return; | |
// flip current bit | |
str[i] = str[i] == '0' ? '1' : '0'; | |
magic(str, i-1, changesLeft-1); | |
// or don't flip it (flip it again to undo) | |
str[i] = str[i] == '0' ? '1' : '0'; | |
magic(str, i-1, changesLeft); | |
} | |
int main(void) { | |
char str[] = "011"; | |
printf("%s\n", str); | |
size_t len = strlen(str); | |
size_t maxDistance = len; | |
for (size_t i = 1 ; i <= maxDistance ; ++i) { | |
printf("Computing for distance %d\n", i); | |
magic(str, len-1, i); | |
printf("----------------\n"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
011
Computing for distance 1
010
001
111
Computing for distance 2
000
110
101
Computing for distance 3
100