真值表输入生成
对于给定的 n 个输入,我需要使用 C++ 生成所有可能的输入组合
eg. n =4
I need to get,
1010101010101010
1100110011001100
1111000011110000
1111111100000000
(编辑:如果不清楚,这些是按列读取的输入组合)
我需要这些来执行类似 & 的操作。和 |所以最好将它们作为 n 个不同的变量以整数表示形式。
我尝试使用 bitset 来处理 32 个输入组合,但处理时间很长。我希望你们对更好的实施有什么想法吗?
EDIT : Example when n=3
10101010
11001100
11110000
For given n inputs, I need to generate all possible input combinations using C++
eg. n =4
I need to get,
1010101010101010
1100110011001100
1111000011110000
1111111100000000
(EDIT : In case this is not clear, these are the input combinations read column-wise)
I need these to perform operations like & and | so it would be best if I get them in their integer representation as n different variables.
I tried doing it using bitset for 32 input combinations but it took a long time to process. I was hoping if you guys had any ideas on a better implementation?
EDIT : Example when n=3
10101010
11001100
11110000
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你的问题对我来说仍然完全无法理解。
但是,下面的代码会重现您的示例输出。
现在我希望这不是家庭作业。如果是这样,那么你通过在 SO 上寻求答案来欺骗自己和他人(这会在以后伤害你和其他人)。对于作业,请明确表明这是作业,然后我们可以相应地调整我们的答案。
干杯&呵呵,
Your question is still utterly incomprehensible to me.
The code below, however, reproduces your example output.
Now I hope this wasn't homework. If it was then you're deceiving yourself and others by seeking answers on SO (which will bite you, and others, later). For homework, please indicate clearly that it is homework, then we can adjust our answers correspondingly.
Cheers & hth.,
这是生成该输出的简短实现:
Here is a short implementation that generates that output:
n=4 不会是
你生成的(不确定“n”是什么意思)???
n=4 would be
not what you generated (not sure what you mean by "n")???
你的问题没有多大意义,但希望这在某种程度上符合你的要求:
所以 n 是你所说的 n,total 是可以保存在 n (3) 位中的可能性总数。然后设置一个 char 数组,如果太短,可以使用 int。所有可能的都被添加了。这是因为真值表的可能性与那么多位中的可能性相同。
第二个 printf 显示 AND 运算,这里我们将执行以下操作:
请参阅 Alfs 代码以您想要的方式进行格式化(尤其是二进制而不是十进制)
Your question doesn't make that much sense but hopefully this is somewhat to what your looking for:
So n is your n as you said, total is the total number of possibilities that can be held in n (3) bits. Then a char array is setup, you can use int if this is too short. All possibles are added. This is because a truth tables possibilities are the same as that of what can be in those many bits.
The second printf shows an AND operation, here we would be doing e.g.:
See Alfs code for formatting in the way you want (especially in binary not in decimal like this)