Java的:真正的发电机和放大器;假的组合给予数N; [英] Java: Generator of true's & false's combinations by giving the number N;

查看:173
本文介绍了Java的:真正的发电机和放大器;假的组合给予数N;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我绑简化尽可能多的任务,这样我就可以把它应用到我的算法。

I tied to simplify the task as much as possible, so I could apply it to my algorithm.

和这里是数学家和程序员的挑战:

And here is the challenge for mathematicians and programmers:

我需要建立在我传递参数整数N A方式:

I need to create a method where I pass parameter int n:

public void optionality_generator(int n){
  //some kind of loops, or recursions...to make it workable
  System.out.println("current combination: ...");
}

输出应显示真实的和虚假的所有可能的组合。

The output should show all possible combinations of true's and false's.

下面是实施例,其中N = 1; N = 2; N = 3; N = 4; N = 5,其中x =虚假和0 =真实的;请注意,空断裂线是只为你认识更容易的模式。但愿,我包括所有可能的组合):

Here is examples where N=1; N=2; N=3; N=4; N=5 where x=false and 0=true; Please note, empty break lines is just for you to recognise easier the patterns. Hopefully, I included all possible combinations):

Combination of 1:
0
x

Combination of 2:
00
x0
0x
xx

Combination of 3:
000
X00
0X0
00X
XX0
0XX
XXX

Combination of 4:
0000

X000
0X00
00X0
000X

XX00
X0X0
X00X

0XX0
0X0X

00XX

XXX0
XX0X
X0XX
0XXX

XXXX

Combination of 5:
00000
X0000
0X000
00X00
000X0
0000X

XX000
X0X00
X00X0
X000X

X0X00
X00X0
X000X

0XX00
0X0X0
0X00X

00XX0
00X0X

000XX

XXX00
XX0X0
XX00X

X0XX0
X0X0X
X00XX

0XXX0
0XX0X

00XXX

XXXX0
XXX0X
XX0XX
X0XXX
0XXXX

XXXXX

此外,如果你看到的输出,这里是我认的模式,即所有组合都在一半反转(如第一种组合是00000最后一个将是XXXXX,第二个X0000,一个是最后一个之前将0XXXX等..)。也许,这种模式将有助于使整个算法更高效,不知道这件事。
谢谢你在前进!

Also, If you see the output, here is the pattern I recognized, that all combinations are inverted on half (e.g first combination is 00000 last one will be XXXXX, second one X0000, one before the last one will be 0XXXX etc..). Maybe, this pattern will help to make the whole algorithm more efficient, not sure about this. Thank you in advance!

推荐答案

下面是一个使用非常基本的方式只的Java API:

Here is a really basic way using only Java APIs:

final int n = 3;
for (int i = 0; i < Math.pow(2, n); i++) {
    String bin = Integer.toBinaryString(i);
    while (bin.length() < n)
        bin = "0" + bin;
    System.out.println(bin);
}

结果:

000
001
010
011
100
101
110
111

当然,你可以设置 N 来任何你喜欢的。而且,对这一结果,你可以挑选 N 日从字符串字符作为真/假。

Of course, you can set n to whatever you like. And, with this result, you can pick the nth character from the string as true/false.

如果你只需要检查了一下是真的,你并不需要将其转换为字符串。这仅仅是为了说明的输出值

If you only need to check if a bit is true, you don't need to convert it to a string. This is just to illustrate the output values.

这篇关于Java的:真正的发电机和放大器;假的组合给予数N;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆