生成汉明距离t内的所有位序列

生成汉明距离t内的所有位序列,第1张

生成汉明距离t内的所有位序列
#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----------------


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5623121.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存