Here we will see all possible binary numbers of n bit (n is given by the user) where the sum of each half is same. For example, if the number is 10001 here 10 and 01 are same because their sum is same, and they are in the different halves. Here we will generate all numbers of that type.
Algorithm
genAllBinEqualSumHalf(n, left, right, diff)
left and right are initially empty, diff is holding difference between left and right
Begin if n is 0, then if diff is 0, then print left + right end if return end if if n is 1, then if diff is 0, then print left + 0 + right print left + 1 + right end if return end if if 2* |diff|Example
#include using namespace std;//left and right strings will be filled up, di will hold the difference between left and rightvoid genAllBinEqualSumHalf(int n, string left="", string right="", int di=0) { if (n == 0) { //when the n is 0 if (di == 0) //if diff is 0, then concatenate left and right cout输出
100001100010101011110011100100101101101110110101110110111111登录后复制
以上就是长度为n的所有可能的二进制数,两半部分的和相等?的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2584048.html