# 一道有关于字符串的算法题，有没有人会？

0

```public class PublicClass
{
private string strReturn = string.Empty;
private static int Count = 0;

/// <summary>
/// 组合算法
/// </summary>
/// <param name="s1">必选项</param>
/// <param name="num">非必选项</param>
/// <returns></returns>
public string exec(string s1, string num)
{
string str = string.Empty;
string[] spNum = num.Split(new char[] { ',' });
str += showFirst(spNum);
for (int i = 1; i <= spNum.Length; i++)
{
if (i != 1 && i != spNum.Length)
{
split(num.Split(new char[] { ',' }), i);

}

}

str += strReturn;
str += num;
return append(s1, str);
}
/// <summary>
///
/// </summary>
/// <param name="str">传入要组合的数组</param>
/// <param name="numcount">分担的数量</param>
private void split(string[] str, int numcount)
{
if (numcount < str.Length)
{
for (int i = 0; i < str.Length; i++)
{
Down(str, str[i], numcount, i + 1);
}

}

}

private string showFirst(string[] num)
{
StringBuilder str = new StringBuilder();
for (int i = 0; i < num.Length; i++)
{
if (!string.IsNullOrEmpty(num[i]))
str.AppendFormat("{0}|", num[i]);
}
return str.ToString();

}

/// <summary>
/// 递归算法
/// </summary>
/// <param name="str">传入的要组合的数组</param>
/// <param name="var">传入的拼接字符串</param>
/// <param name="numcount">分担的数量</param>
/// <param name="num">循环开始的大小</param>
private void Down(string[] str, string var, int numcount, int num)
{
for (int i = num; i < str.Length; i++)
{
string Return = var + "," + str[i];
if (numcount - i <= 1)//代表循环到最后一位i
{
if (Return.Split(new char[] { ',' }).Length == numcount)
strReturn = strReturn + Return + "|";
else
Down(str, Return, numcount, i + 1);

}
else
Down(str, Return, numcount, i + 1);

}

}

private string append(string s1, string append)
{
StringBuilder str = new StringBuilder();
str.AppendFormat("0|", s1);
Count = Count + 1;
string[] split = append.Split(new char[] { ',' });
for (int i = 0; i < split.Length; i++)
{
if (!string.IsNullOrEmpty(split[i]))
{
Count = Count + 1;
str.AppendFormat("{0},{1}|", s1, split[i]);

}

}
return str.ToString();

}

private int CountSum(string str)
{
int sumcount = 0;
string[] s = str.Split(new char[] { ',' });
foreach (string s1 in s)
{
if (!string.IsNullOrEmpty(s1))
sumcount = sumcount + 1;

}

return sumcount;

}

}```
zhibolife | 菜鸟二级 | 园豆：202

您需要登录以后才能回答，未注册用户请先注册