多选题答案组合算法
这两天要做一个多选题答案的库 ,本来想着从网上当点算法下来的,找了找没什么合适的,就自己写了个:答案选项是3-8个,
const string charlist = "ABCDEFGH";
List<string> list = new List<string>();
public string GetMulChooseAnsList()
{
StringBuilder sql = new StringBuilder("");
for (int i = 3; i <=8; i++)
{
//int i = 4;
for (int j = 0; j < i; j++)
{
//答案为两个个选项的情况
string basestr = charlist[j].ToString();
for (int n = j + 1; n < i; n++)
{
if (basestr.IndexOf(charlist[n]) >= 0)
{
continue;
}
string tmp = basestr + charlist[n].ToString(); list.Add(tmp);
Recursive(tmp, n,i);
}
}
}
return sql.ToString();
}
public void Recursive(string bastr,int n,int currentnum)
{
if (n > currentnum) return ;
string tmp = "";
for (int i = n + 1; i <currentnum; i++)
{
tmp = bastr + charlist[i].ToString();
list.Add(tmp);
Recursive(tmp, i, currentnum);
}
return ;
}