给定n个数组,我们如何计算给定范围内该数组的子集(基于0的索引),即从第i个索引到第j个索引。我尝试使用bitmasking,但由于范围无法弄清楚如何解决这个问题。
例如,如果数组a是a = [2 6 9 1 7]且给定范围是1到3,那么答案将是= [6],[9],[1],[6 9] ,[6 1],[9 1],[6 9 1]
这是计算所有数组子集的函数,我不确定如何使用该范围约束。
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim cn As New SqlConnection
Dim cmd As New SqlCommand
Dim dr As SqlDataReader
cn.ConnectionString = "Server=localhost;Database=test;Uid=sa;Pwd=fadyjoseph21"
cmd.Connection = cn
cmd.CommandText = "INSERT INTO test2(Username,Password) VALUES('" & TextBox1.Text & "','" & TextBox2.Text & "')"
cn.Open()
dr = cmd.ExecuteReader
If dr.HasRows Then
MsgBox("You're already registered")
Else
MsgBox("Already registered")
End If
End Sub
答案 0 :(得分:0)
如果您当前的findSubSets
实现有效,那么转换为范围几乎是微不足道的。只需包含等于i
的偏移索引,并将array.length
更改为j - i + 1
。
private static void findSubsets(int array[], int i, int j)
{
int arrayLen = j - i + 1;
int numOfSubsets = 1 << (arrayLen - 1);
for (int k = 1; k < numOfSubsets; k++)
{
int pos = j;
int bitmask = k;
System.out.print("{");
while (bitmask > 0)
{
if ((bitmask & 1) == 1)
System.out.print(array[pos] + ",");
bitmask >>= 1;
pos--;
}
System.out.print("}");
}
}