Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 布尔变量的所有可能组合_C#_Algorithm_Boolean - Fatal编程技术网

C# 布尔变量的所有可能组合

C# 布尔变量的所有可能组合,c#,algorithm,boolean,C#,Algorithm,Boolean,我正试图找到一种算法,使我能够执行以下操作: 假设我有10个布尔变量,我想尝试每一个组合,因为我的目标是找到任何组合,作为我的一个方法的结果为真(这种方法有很多限制,这就是为什么我想测试每一种可能的组合,如果没有解决问题的组合,那么我想通知用户这一点)。 我希望这是可以理解的!试试这个: for (int i = 0; i < (1 << 10); i++) { bool b1 = (i & (1 << 0)) != 0; bool b2 =

我正试图找到一种算法,使我能够执行以下操作: 假设我有10个布尔变量,我想尝试每一个组合,因为我的目标是找到任何组合,作为我的一个方法的结果为真(这种方法有很多限制,这就是为什么我想测试每一种可能的组合,如果没有解决问题的组合,那么我想通知用户这一点)。 我希望这是可以理解的!

试试这个:

for (int i = 0; i < (1 << 10); i++)
{
    bool b1 = (i & (1 << 0)) != 0;
    bool b2 = (i & (1 << 1)) != 0;
    bool b3 = (i & (1 << 2)) != 0;
    bool b4 = (i & (1 << 3)) != 0;
    ...

    if (MyMethod(b1, b2, b3, b4, ...))
    {
        // Found a combination for which MyMethod returns true
    }
}

定义如下所示的类:

class Bint
{
  int num;
  public bool this[int num]
  {
    get {return num << n & 0x1 == 1;}
  }
  public int Num
  {
    get {return num;}
    set {num = value;}
  }
}
类Bint
{
int-num;
公共bool this[int num]
{

get{return num我终于想出了一个更有效的方法:使用二进制数: 假设我想测试8个变量中所有可能的布尔组合: 如果我选择执行以下操作,我将测试每个组合:

public string CombinationFinder()
{
    for (int i = 0; i < 2 ^ 8; i++)
    {
        String ans = Convert.ToInt32(i, 2).ToString();
        if (myMethod(ans))
        {
            return ans;
        }
    }
    return null;
}
公共字符串组合查找器()
{
对于(int i=0;i<2^8;i++)
{
字符串ans=Convert.ToInt32(i,2).ToString();
if(我的方法(ans))
{
返回ans;
}
}
返回null;
}
这将从0到255,二进制表示从00000000到11111111
其中每个数字都取0或1,可以表示为布尔值。在本例中,如果没有找到组合,则该方法将返回null。

我知道这是一个老问题,但waclock的答案无法编译(C#中没有指数运算符).dtb的答案有99%的正确率,但不处理未知数量的布尔值,这就是这个答案所提供的:

var props = typeof(TypeWithBooleans).GetProperties().Where(prop => prop.PropertyType == typeof(bool)).ToArray();

for (var i = 0; i < (1 << props.Length); ++i)
{
    var combination = Enumerable.Range(0, props.Length).Select(j => (i & (1 << j)) != 0).ToArray();
    if (MyMethod(combination)) {
        // handle match
    };
}
var props=typeof(TypeWithBooleans).GetProperties().Where(prop=>prop.PropertyType==typeof(bool)).ToArray();

对于(var i=0;i<(1)(我和(1)我要睡觉了,快凌晨3点了,我必须早起,明天会让你知道,但从我看来,我认为它可能会起作用!:)谢谢有任何方法可以应用,但要应用到一些X变量(意味着我不知道我会有多少布尔值,它们取决于用户选择).我对语法不太熟悉,这就是为什么我要问:)
public string CombinationFinder()
{
    for (int i = 0; i < 2 ^ 8; i++)
    {
        String ans = Convert.ToInt32(i, 2).ToString();
        if (myMethod(ans))
        {
            return ans;
        }
    }
    return null;
}
var props = typeof(TypeWithBooleans).GetProperties().Where(prop => prop.PropertyType == typeof(bool)).ToArray();

for (var i = 0; i < (1 << props.Length); ++i)
{
    var combination = Enumerable.Range(0, props.Length).Select(j => (i & (1 << j)) != 0).ToArray();
    if (MyMethod(combination)) {
        // handle match
    };
}