#include <stdio.h>#include <stdint.h>#include <string.h>void magic(char* str, int i, int changesLeft) { if (changesLeft == 0) { printf("%sn", 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("%sn", str); size_t len = strlen(str); size_t maxDistance = len; for (size_t i = 1 ; i <= maxDistance ; ++i) { printf("Computing for distance %dn", i); magic(str, len-1, i); printf("----------------n"); } return 0;}
输出:
MacBook-Pro:hammingDist gsamaras$ nano kastrinis.cppMacBook-Pro:hammingDist gsamaras$ g++ -Wall kastrinis.cpp MacBook-Pro:hammingDist gsamaras$ ./a.out 011Computing for distance 1010001111----------------Computing for distance 2000110101----------------Computing for distance 3100----------------
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)