真值表输入生成

发布于 2024-10-03 02:52:11 字数 395 浏览 6 评论 0原文

对于给定的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

等风也等你 2024-10-10 02:52:11

你的问题对我来说仍然完全无法理解。

但是,下面的代码会重现您的示例输出。

#include <iostream>

int main()
{
    using namespace std;

    int const   n       = 3;
    int const   nBits   = 1 << n;

    int powerOf2  = 1;
    for( int i = 0;  i < n;  ++i )
    {
        for( int bitNum = 0;  bitNum < nBits;  ++bitNum )
        {
            cout << 1 - bitNum/powerOf2 % 2;
        }
        cout << endl;
        powerOf2 *= 2;
    }
}

现在我希望这不是家庭作业。如果是这样,那么你通过在 SO 上寻求答案来欺骗自己和他人(这会在以后伤害你和其他人)。对于作业,请明确表明这是作业,然后我们可以相应地调整我们的答案。

干杯&呵呵,

Your question is still utterly incomprehensible to me.

The code below, however, reproduces your example output.

#include <iostream>

int main()
{
    using namespace std;

    int const   n       = 3;
    int const   nBits   = 1 << n;

    int powerOf2  = 1;
    for( int i = 0;  i < n;  ++i )
    {
        for( int bitNum = 0;  bitNum < nBits;  ++bitNum )
        {
            cout << 1 - bitNum/powerOf2 % 2;
        }
        cout << endl;
        powerOf2 *= 2;
    }
}

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.,

丿*梦醉红颜 2024-10-10 02:52:11

这是生成该输出的简短实现:

void print_mask(int n){
    for (int level = 0; level < n; level++){
        for (int i = (1<<n)-1; i>=0; i--)   // we'll always output 2**n bits
            printf("%d", (i >> level) & 1);
        printf("\n");
    };
};

Here is a short implementation that generates that output:

void print_mask(int n){
    for (int level = 0; level < n; level++){
        for (int i = (1<<n)-1; i>=0; i--)   // we'll always output 2**n bits
            printf("%d", (i >> level) & 1);
        printf("\n");
    };
};
灯角 2024-10-10 02:52:11

n=4 不会是

0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111

你生成的(不确定“n”是什么意思)???

n=4 would be

0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111

not what you generated (not sure what you mean by "n")???

云仙小弟 2024-10-10 02:52:11

你的问题没有多大意义,但希望这在某种程度上符合你的要求:

#include <stdio.h>

main(){

        int n = 3;
        int total = (n*n)-1;
        char poss[total];
        int i = 0;
        for (i = 0; i < total; ++i){
                poss[i] = i;
                printf("%d \t",poss[i]);
                printf("%d \n",poss[i] & 1);
        }

}

所以 n 是你所说的 n,total 是可以保存在 n (3) 位中的可能性总数。然后设置一个 char 数组,如果太短,可以使用 int。所有可能的都被添加了。这是因为真值表的可能性与那么多位中的可能性相同。

第二个 printf 显示 AND 运算,这里我们将执行以下操作:

000 AND 001 => 000
001 AND 001 => 001
010 AND 001 => 000
011 AND 001 => 001

请参阅 Alfs 代码以您想要的方式进行格式化(尤其是二进制而不是十进制)

Your question doesn't make that much sense but hopefully this is somewhat to what your looking for:

#include <stdio.h>

main(){

        int n = 3;
        int total = (n*n)-1;
        char poss[total];
        int i = 0;
        for (i = 0; i < total; ++i){
                poss[i] = i;
                printf("%d \t",poss[i]);
                printf("%d \n",poss[i] & 1);
        }

}

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.:

000 AND 001 => 000
001 AND 001 => 001
010 AND 001 => 000
011 AND 001 => 001

See Alfs code for formatting in the way you want (especially in binary not in decimal like this)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文